Which of the following commands can be used to update the last modified timestamp on a file or if the file specified does not exist will create the file?
Every file in Linux is associated with timestamps, which specifies the last access time, last modification time and last change time. Show
Whenever we create a new file, or modify an existing file or its attributes, these timestamps will be updated automatically. Touch command is used to change these timestamps (access time, modification time, and change time of a file). 1. Create an Empty File using touchYou can create an empty file using touch command. The following example will create a zero byte new file named tgs.txt. $ touch tgs.txtYou can also use -c option to avoid creating new files. If you use -c option, and if a file doesn’t exists, touch will not create the file. $ touch -c a.txtCommands like ls command and find command uses these timestamp information for listing and finding files. You can also create more than 1 files from a single touch command. The following example will create 4 files named a, b, c, and d. $ touch a b c d2. Change File’s Access Time using -aWe can change the access time of a file using -a option. By default it will take the current system time and update the atime field. Before touch command is executed: $ stat tgs.txt File: `tgs.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 394283 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan) Access: 2012-10-18 23:58:21.663514407 +0530 Modify: 2012-10-18 23:58:21.663514407 +0530 Change: 2012-10-18 23:58:21.663514407 +0530$ touch -a tgs.txtAfter the above touch command (Please note that the access time is changed): $ stat tgs.txt File: `tgs.txt' Size: 0 Blocks: 0 IO Block: 4096 regular empty file Device: 801h/2049d Inode: 394283 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan) Access: 2012-10-19 00:08:23.559514525 +0530 Modify: 2012-10-18 23:58:21.663514407 +0530 Change: 2012-10-19 00:08:23.559514525 +05303. Change File’s Modification Time using -mYou can change the modification time of a file using -m option. $ touch -m *.oThe above method can be used to change the mtime of all obj files, when using make utility. NOTE: It is not possible to change the ctime using touch command 4. Explicitly Setting Access and Modification time using -t and -dInstead of taking the current time-stamp, you can explicitly specify the time using -t and -d options. The format for specifying -t is [[CC]YY]MMDDhhmm[.SS] $ touch -t [[CC]YY]MMDDhhmm[.SS]The following explains the above format:
For example: Verify the above change using stat command: $ stat tgs.txt File: `tgs.txt' Size: 3 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 394283 Links: 1 Access: (0644/-rw-r--r--) Uid: ( 1000/lakshmanan) Gid: ( 1000/lakshmanan) Access: 2038-01-18 12:05:09.000000000 +0530 Modify: 2038-01-18 12:05:09.000000000 +0530 Change: 2012-10-19 00:40:58.763514502 +0530You can also use a string to change the time Another example: $ touch -d "2012-10-19 12:12:12.000000000 +0530" tgs.txtFor developers, touch command will be really helpful when you are working with Makefiles 5. Copy the Time-stamp from Another File using -rYou can also take a file as a reference, and update the time for other files, so that both file will hold the same time. The following touch command example will update the time-stamp of file a.txt with the time-stamp of tgs.txt file. $ touch a.txt -r tgs.txtView Discussion Improve Article Save Article View Discussion Improve Article Save Article The touch command is a standard command used in UNIX/Linux operating system which is used to create, change and modify timestamps of a file. Basically, there are two different commands to create a file in the Linux system which is as follows:
Using touch CommandInitially, we are in home directory and this can be checked using the pwd command. Checking the existing files using command ls and then long listing command(ll) is used to gather more details about existing files. As you can see in the below figure there is no existing files. Touch command Syntax to create a new file: You can create a single file at a time using touch command. Syntax: touch file_nameThe file which is created can be viewed by ls command and to get more details about the file you can use long listing command ll or ls -l command . Here file with name ‘File1‘ is created using touch command. Touch command to create multiple files: Touch command can be used to create the multiple numbers of files at the same time. These files would be empty while creation. Syntax: touch File1_name File2_name File3_nameMultiple files with name Doc1, Doc2, Doc3 are created at the same time using touch command here. touch Command OptionsLike all other command Touch command have various options. These options are very useful for various purpose. touch -a: This command is used to change access time only. To change or update the last access or modification times of a file touch -a command is used. Syntax: Here touch -a command changes access time of the file named Doc1. touch -c : This command is used to check whether a file is created or not. If not created then don’t create it. This command avoids creating files. Syntax: touch -c fileNametouch -c-d : This is used to update access and modification time. Syntax: touch -c-d fileNametouch -m : This is used to change the modification time only. It only updates last modification time. Syntax: touch -m fileNametouch -r : This command is used to use the timestamp of another file. Here Doc2 file is updated with the time stamp of File 1. Syntax: touch -r second_file_name first_file_nametouch -t : This is used to create a file using a specified time. Syntax: touch -t YYMMDDHHMM fileNameWhich of the following commands can be used to recursively search through the directory tree in search?You can use grep command or find command as follows to search all files for a string or words recursively.
Which of the following commands will help find you all the files in current directory its subdirectories?The ls command is used to list files or directories in Linux and other Unix-based operating systems. Just like you navigate in your File explorer or Finder with a GUI, the ls command allows you to list all files or directories in the current directory by default, and further interact with them via the command line.
What command can be used to display the last five lines of a text file?To look at the last few lines of a file, use the tail command. tail works the same way as head: type tail and the filename to see the last 10 lines of that file, or type tail -number filename to see the last number lines of the file. Try using tail to look at the last five lines of your .
Which of the following commands lets you move to another directory choose only one answer?cd command in linux known as change directory command. It is used to change current working directory.
|