While it may seem antiquated in this day and maturat of Bodoni font operating systems, the command line remains the well-nig flexile and regnant way to perform tasks in Linux. The full guide to the Linux command line discusses some of the very bedroc of the Linux overtop line including directory navigation, file/directory operations and hunting. Therein follow up article we anatomy on it as we go deeper to discuss file metadata, permissions, timestamps, likewise equally some new-sprung tools like tee, Vim, and more.

13. How to admittance file metadata like size, permissions, and more?

Use the ls command with -l option to display file metadata in output. For example:

Each line in the output contains metadata information relevant to a file or a sub-directory here in the current directory. This selective information can be distributed into the following eight parts:

+permissions that apply to the owner
|
| +permissions that apply to entirely other users
| |
| | +number of hard links
| | |
| | | +file size +last modification engagement/meter
_|_ _|_ | _|___ ________|__
drwx r-x r-x 3 himanshu himanshu 4096 Jul 3 14:26 Desktop
__ ________ ________ ______
| | | |
| | | +call of file/ directory
| | |
| | +radical the file belongs to
| |
| +possessor of the file
|
+permissions that put on to the members of the group the file in belongs to

The first character represents the file eccentric. For example, in the line shown preceding, d indicates this is a directory. Some other values can be: - for normal Indian file, s for socket data file, l for link file, and more.

The next 9 characters lay out permissions -- r - read, w - drop a line, x - execute. The first determined of three characters represents the owner's permission, the next three are group's permission, and the final three act permissions granted to others who are neither the owner, nor the part of the group the file belongs to. In the example shown above, the owner has translate, drop a line and execute permissions, while the radical as well as others both let only learn and execute permissions.

Fee: Use the -h command origin pick along with -l to display file size in human legible format.

14. How to change file permissions?

Use the chmod command to alter register permissions. On that point are ii slipway in which this command nates equal used. The starting time method, alias letters method acting, uses + , - , and = signs to add, remove, and ascribe permissions. Letters a , o , u , and g represent all, others, possessor, and group severally.

For representative, the chmod u=rwx somefile command assigns read, write out, and execute permissions to the owner of the file somefile. Likewise, the chmod o+w somefile compel adds write permission for others, the chmod g-r somefile removes read license from the group the lodge belongs to, and the chmod a+x somefile dictation adds execute license for everyone.

Specifying a is not mandatory, which substance that setting permissions like +x surgery -r without specifying possessor, aggroup or other mechanically applies it to all.

The second gear method is the numbers method and it uses 4 , 2 , and 1 or else of r, w, and x. The values are added together in sets of 3 to give us a three fingerbreadth come denoting permissions.

For example, the chmod 761 somefile command gives rwx, rw, and r permissions to the owner, group, and others, respectively. Here 7 represents the heart of numbers corresponding to r,w, and x. Similarly, 6 represents the sum of numbers corresponding to r and w, while 1 represents x.

15. How to change file timestamps?

Use the touch command to transfer file timestamps. There are three types of timestamps associated with a data file: Memory access time, Modification sentence, and Change time. While the first two are self explanatory, the ordinal one represents the time when the inode information or the meta data overlapping data file last changed. Use the stat command to display these timestamps:

To change the file entree time to the current time, use the rival command with the -a option: contact -a somefile . Similarly, the -m option changes the file qualifying time to the current clip.

To change file timestamps to a time other than the current time, use the -t command line option. For example, the command touch -t 201407020900.01 -a somefile changes the admittance timestamp of somefile to 2014/07/02 09:00:01. You can besides pass a specific date and time in quality clear form. Use the -d command line option for this. Here are any examples:

touch -d "2013-01-10 10:00:07" -a somefile

touch -d "next Dominicus" -m somefile

touch -d "3 hours past" -a somefile

16. How to determine file types?

Practice the file command to determine file types. As shown in the example below, the command expects a file name as an argument. You can also use the wildcard * in place of charge describ to video display the file type for all file in the present-day directory: file *

17. I've downloaded an executable charge, merely information technology doesn't execute, why?

In Linux (and other *nix systems) whether a charge is executable or non depends solely on its permissions, non on its extension or content. When a file is downloaded, its novel permissions are not known, and is hence given a nonremittal set of permissions that are determined by umask.

If the user really intends to execute the downloaded file, they'll have to explicitly founde executable permissions to IT using the chmod command explained above. Giving permissions manually also helps prevent computer virus, worms, and more from infecting your system without your cognition.

18. How to print the number of new lines, words, and bytes in files?

Exercise the wc command to photographic print newline, word, and byte counts for a file away. Here is an example:

In the output shown supra, 5 represents the number of lines, 12 represents the figure of words, and 52 represents the number of bytes. You buns also use the -l , -w , and -c command line options to singly produce number of lines, words, and bytes, respectively in the end product.

19. How to presentation disk utilization of files and directories?

Exercise the du overtop to video display disk exercis of files and directories. Hither is an example:

Note - The -h command line option is accustomed produce the size in human clear initialize.

An important thing to distinction present is that the du command outputs the resident size of a file, which could be variant from the actual size of it that the ls -l instruction displays. The reason behind this difference is either slack distance or sparse files.

To exhibit the combined size of a directory as well as all its subdirectories, use the -s option, while -S can be wont to showing tell apart sizes. To display the number of disk space available on the file away organization containing a specialized file or directory utilisation the df dominate.

Here over again, the -h pick is used to display the yield in human readable data formatting. If the df command is consort without some lodge/directory name, it'll show magnetic disk usage for all the file systems.

20. How to comparability two files?

Use the diff command to compare two files. The command examines some the files and produces the output in a particular arrange to Lashkar-e-Toiba you know what changes are obligatory for the files to match. The bid requires two filenames as arguments, as shown in the example below.

Use the diff command to compare these files:

Decrypting the output signal shown preceding, 5c5 means that the one-fifth logical argument of somefile is changed, and should be replaced by the one-fifth channel of the file anotherfile. The air in question from the first file in is noticeable with a < symbol, while line from the second file is noticeable with a > symbolization.

Note - As well c , which signifies a transformed line, the diff command also points which lines need to be added ( a ) and deleted ( d ) for the files being compared to match. More complex examples of this command can be found here.

21. How to view the first few and last hardly a lines of a lodge?

Use the point and tail commands to quickly view the above all a couple of lines of a file. These commands enter upon handy when you just want to have a warm peek inside the file in. For example, the head -n2 somefile command displays the first 2 lines of the file somefile. Similarly, the tush -n3 somefile command displays the last 3 lines of the Indian file.

Not only lines, you can also quickly view a specified number of bytes using these commands. For this, use the -c command line option instead of -n. By default, when the number of lines is non specified, both the commands exhibit 10 lines in the output.

22. How to store and view the production of a command at once?

Use the tee command to simultaneously compose the output of any early statement to standard production also as to uncomparable surgery more files. For example, the ls | tee up ls-dump control displays the output of the ls statement on console and stores the output in the file ls-dump.

While the tee command is mostly used for capturing and analyzing logs at the same time, it can also be used to speed up your work flow. For lesson, the echo "Linux command line" | teeing ground file1 > file2 command writes the string to some files in one go.

23. How to compress and uncompress a file?

Functional along Linux requires you to deal with archives like .Jack-tar , .tar.gz , .bz2 , and more. To create as well as decompress these archives you can use the seaman command.

E.g., the tar -cvf practice.tar practice session/ command compresses the practice pamphlet and creates a .tar archive named practice.seafarer. The -c command line option tells the tar command to create an archive, -v displays the files added to the tarball , and -f specifies the filename.

To uncompress the .tar archive created above, use the tar -xvf practice.tar command. The -x command line pick signals the command to extract the archive. This command untars the file in the ongoing directory. Enjoyment the -C selection to specify a antithetical target directory.

To create .tar.gz and .tar.bz2 archives, add an supernumerary -z and -j require line option, respectively. The command to uncompress these archives is same as the one used for .tar files. Use the -t command telephone line selection (on with v and f) in case you just wish to list the table of contents of an archive.

Lead - To deal with .zip files, use the zip command.

24. How to edit a filing cabinet using Vim editor program?

While the Vim editor is one of the just about powerful command line textual matter editors, it besides requires you to learn a lot of keyboard shortcuts. Just the basics of editing are simple and easy.

To yawning a file out in the editor, run the muscularity command with the file name as an argument. For example, vim textfile . If the file textfile doesn't live in the specified directory, the editor will make up and open a new file aside that name, otherwise it wish open the existing file.

There are two surgery modes in Vim: overtop musical mode and insert manner . The editor opens the file in overtop mode, where you can move the cursor using the pointer keys happening your keyboard, but cannot edit the file until you urge on i -- activating the insert fashion As shown under.

Once you are done redaction the charge, you bear to press the Esc identify to come out of the insert modality and into the command mode before being able to save the file.

To save the file, case the :w command and then hit Enter .

To quit the editor, typewrite the :q command and press Insert, or :wq to save and quit in one go around.

Note - To promptly copy or delete a line, switch the editor program to the command mode, bring the pointer to the desired line, and type yy or dd , respectively. To paste, press p in the command mode.

Wrap

Each command mentioned in the clause is capable of doing much to a higher degree what we've discussed, but this should give you an overall grasp of their use. You can use man pages -- as discussed in part one -- to learn more about them.

If you get stuck somewhere, you can leave a comment in our Software assembly.