A Beginner's Guide to the Linux Command Line, Part II
While it may seem antiquated in this mean solar day and age of mod operating systems, the command line remains the most flexible and powerful fashion to perform tasks in Linux. The full guide to the Linux command line discusses some of the very basics of the Linux command line including directory navigation, file/directory operations and search. In this follow upwardly article we build on that as nosotros go deeper to hash out file metadata, permissions, timestamps, as well as some new tools like tee, Vim, and more.
13. How to access file metadata like size, permissions, and more?
Use the ls command with -l option to display file metadata in output. For instance:
Each line in the output contains metadata data related to a file or a sub-directory present in the current directory. This information can be divided into the post-obit eight parts:
+permissions that utilize to the possessor
|
| +permissions that apply to all other users
| |
| | +number of hard links
| | |
| | | +file size +last modification date/fourth dimension
_|_ _|_ | _|___ ________|__
drwx r-x r-10 3 himanshu himanshu 4096 Jul 3 14:26 Desktop
__ ________ ________ ______
| | | |
| | | +name of file/ directory
| | |
| | +group the file belongs to
| |
| +possessor of the file
|
+permissions that apply to the members of the group the file belongs to
The commencement graphic symbol represents the file type. For example, in the line shown in a higher place, d indicates this is a directory. Other values can exist: - for normal file, southward for socket file, fifty for link file, and more.
The adjacent 9 characters represent permissions -- r - read, westward - write, x - execute. The start set of three characters represents the owner's permission, the next three are group's permission, and the terminal 3 represent permissions granted to others who are neither the possessor, nor the office of the group the file belongs to. In the example shown higher up, the owner has read, write and execute permissions, while the group as well as others both have only read and execute permissions.
Tip: Utilize the -h command line pick along with -fifty to display file size in man readable format.
fourteen. How to alter file permissions?
Use the chmod control to alter file permissions. In that location are two ways in which this command can be used. The get-go method, also known as letters method, uses + , - , and = signs to add together, remove, and assign permissions. Messages a , o , u , and thou represent all, others, owner, and group respectively.
For example, the chmod u=rwx somefile command assigns read, write, and execute permissions to the owner of the file somefile. Similarly, the chmod o+due west somefile control adds write permission for others, the chmod g-r somefile removes read permission from the group the file belongs to, and the chmod a+x somefile control adds execute permission for everyone.
Specifying a is non mandatory, which means that setting permissions like +x or -r without specifying owner, group or other automatically applies it to all.
The second method is the numbers method and it uses 4 , two , and ane instead of r, w, and x. The values are added together in sets of three to give us a iii digit number denoting permissions.
For instance, the chmod 761 somefile command gives rwx, rw, and r permissions to the possessor, group, and others, respectively. Here 7 represents the sum of numbers corresponding to r,due west, and x. Similarly, 6 represents the sum of numbers corresponding to r and west, while 1 represents x.
15. How to change file timestamps?
Apply the bear upon command to change file timestamps. In that location are three types of timestamps associated with a file: Access time, Modification time, and Change time. While the starting time two are self explanatory, the third one represents the time when the inode information or the meta data related to file final inverse. Use the stat command to display these timestamps:
To alter the file access time to the current time, utilise the touch command with the -a choice: bear on -a somefile . Similarly, the -1000 option changes the file modification time to the electric current time.
To change file timestamps to a fourth dimension other than the electric current time, employ the -t command line option. For example, the command affect -t 202207020900.01 -a somefile changes the admission timestamp of somefile to 2014/07/02 09:00:01. You tin as well pass a specific date and time in human readable form. Employ the -d command line pick for this. Here are some examples:
affect -d "2013-01-10 10:00:07" -a somefile
touch -d "next sunday" -k somefile
touch -d "three hours agone" -a somefile
16. How to determine file types?
Use the file command to determine file types. Equally shown in the example below, the command expects a filename as an argument. Y'all can also utilize the wildcard * in place of file proper noun to display the file type for every file in the current directory: file *
17. I've downloaded an executable file, simply it doesn't execute, why?
In Linux (and other *nix systems) whether a file is executable or non depends solely on its permissions, non on its extension or content. When a file is downloaded, its original permissions are non known, and is hence given a default fix of permissions that are determined past umask.
If the user really intends to execute the downloaded file, they'll have to explicitly requite executable permissions to it using the chmod command explained above. Giving permissions manually also helps prevent virus, worms, and more from infecting your system without your knowledge.
18. How to print the number of new lines, words, and bytes in files?
Use the wc command to print newline, word, and byte counts for a file. Here is an example:
In the output shown above, 5 represents the number of lines, 12 represents the number of words, and 52 represents the number of bytes. You can as well employ the -50 , -west , and -c command line options to separately produce number of lines, words, and bytes, respectively in the output.
19. How to display deejay usage of files and directories?
Employ the du command to display disk usage of files and directories. Here is an example:
Annotation - The -h command line option is used to produce the size in human readable format.
An of import thing to note here is that the du control outputs the resident size of a file, which could be different from the actual size that the ls -l command displays. The reason behind this difference is either slack space or sparse files.
To display the combined size of a directory too every bit all its subdirectories, apply the -southward choice, while -South tin be used to display separate sizes. To display the corporeality of deejay infinite available on the file system containing a specific file or directory apply the df control.
Here once more, the -h option is used to display the output in human readable format. If the df command is run without any file/directory proper noun, it'll show disk usage for all the file systems.
xx. How to compare two files?
Utilise the diff command to compare ii files. The control examines both the files and produces the output in a detail format to let you know what changes are required for the files to match. The command requires two filenames as arguments, as shown in the example below.
Use the diff command to compare these files:
Decrypting the output shown above, 5c5 means that the fifth line of somefile is inverse, and should be replaced by the 5th line of the file anotherfile. The line in question from the start file is marked with a < symbol, while line from the 2nd file is marked with a > symbol.
Note - Too c , which signifies a inverse line, the diff control also points which lines need to exist added ( a ) and deleted ( d ) for the files being compared to lucifer. More complex examples of this control can be establish hither.
21. How to view the outset few and concluding few lines of a file?
Apply the head and tail commands to apace view the kickoff and last few lines of a file. These commands come in handy when y'all only want to take a quick peek inside the file. For example, the head -n2 somefile command displays the showtime 2 lines of the file somefile. Similarly, the tail -n3 somefile control displays the last three lines of the file.
Non only lines, you lot can also speedily view a specified number of bytes using these commands. For this, use the -c command line option instead of -n. Past default, when the number of lines is not specified, both the commands brandish 10 lines in the output.
22. How to store and view the output of a command at once?
Apply the tee command to simultaneously write the output of any other command to standard output likewise as to i or more files. For example, the ls | tee ls-dump command displays the output of the ls command 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 fourth dimension, information technology can also exist used to speed up your workflow. For example, the echo "Linux control line" | tee file1 > file2 command writes the string to both files in ane get.
23. How to shrink and uncompress a file?
Working on Linux requires you to deal with archives like .tar , .tar.gz , .bz2 , and more. To create every bit well equally uncompress these athenaeum you can employ the tar command.
For example, the tar -cvf practice.tar exercise/ command compresses the practice folder and creates a .tar archive named practice.tar. The -c command line pick 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 exercise.tar control. The -x command line pick signals the command to excerpt the archive. This command untars the file in the current directory. Utilise the -C option to specify a different target directory.
To create .tar.gz and .tar.bz2 archives, add an extra -z and -j control line pick, respectively. The control to uncompress these archives is same as the i used for .tar files. Use the -t command line option (along with five and f) in case you but desire to listing the contents of an archive.
Tip - To deal with .zilch files, employ the zilch command.
24. How to edit a file using Vim editor?
While the Vim editor is one of the most powerful command line text editors, it also requires you to larn a lot of keyboard shortcuts. Just the basics of editing are simple and easy.
To open a file in the editor, run the vim command with the file name as an argument
In that location are 2 operation modes in Vim: control manner and insert mode . The editor opens the file in control mode, where yous tin can move the cursor using the arrow keys on your keyboard, merely cannot edit the file until you press i -- activating the insert manner as shown below.
Once y'all are done editing the file, y'all have to press the Esc key to come out of the insert mode and into the command mode earlier being able to save the file.
To save the file, type the :w control and then striking Enter .
To quit the editor, type the :q command and press Enter, or :wq to save and quit in 1 go.
Annotation - To rapidly copy or delete a line, switch the editor to the command mode, bring the cursor to the desired line, and type yy or dd , respectively. To paste, press p in the command mode.
Wrap Up
Each command mentioned in the article is capable of doing a lot more than than what nosotros've discussed, but this should requite you an overall grasp of their use. Yous can use man pages -- as discussed in part one -- to learn more nigh them.
If y'all get stuck somewhere, you can leave a comment in our Software forum.
Source: https://www.techspot.com/guides/844-linux-command-line-part-ii/
Posted by: rodgerslacir2002.blogspot.com

0 Response to "A Beginner's Guide to the Linux Command Line, Part II"
Post a Comment