A Beginner's Guide to the Linux Command Line
Practise you think of the command line as an blowsy leftover from the by, or an old fashioned way of interacting with a reckoner? Think over again. In Linux, the command line remains the most flexible and powerful way to perform tasks. For example, searching for all .tmp files in a directory (and its subdirectories) and then deleting them can be a multi-stride process when done via GUI, but it only takes seconds when washed through the control line.
In this commodity, nosotros'll hash out the basics of the Linux control line including directory navigation, file/directory operations, and search. Once you have mastered these, in the 2nd half of this guide, we get deeper to talk over file metadata, permissions, timestamps, and more.
1. What is a home directory in Linux?
Linux is a multi-user operating system, which means that more than one user can access the Bone at the same time. To make things easy, each user is assigned a directory where they can store their personal files. This directory is known as a user's dwelling directory.
Home directories are found under the dwelling house directory. For example, my dwelling house directory is /dwelling/himanshu . Delight note that a user'southward dwelling house directory has the aforementioned name every bit their login name. If y'all are a Windows user, y'all can think of a Linux home directory every bit a user specific directory usually present within C:\Documents and Settings or C:\Users.
Users take complete command over their dwelling house directory as well as all its sub-directories. This means that they can freely perform operations like create and delete files/directories, install programs, and more, inside their domicile directory.
2. How to check the present working directory?
Whenever you open a control line shell in Linux, you start at your home directory. This is your present working directory, which changes as you switch to another directory. Use the pwd control to check the consummate path of your present working directory at whatsoever point of time.
Here is an case:
The pwd command output, shown in the screenshot to a higher place, indicates that the user is currently in the Pictures directory, which is inside the himanshu directory, which in turn is a subdirectory of the home directory. In this example himanshu@ubuntu:~/Pictures$ is the command line prompt.
3. How to switch directories?
Use the cd command to navigate through the Linux filesystem. This command requires either a directory name or its complete path depending upon where the directory is present.
For case, if your present working directory is /home/himanshu/pictures, and y'all want to switch to /domicile/himanshu/pictures/vacations, then you can simply run the command: cd vacations . In this case, the command line shell volition search for the vacations directory within pictures. A path relative to the nowadays working directory is also known every bit relative path.
But in example you want to switch to /home/techspot, yous'll have to run the control: cd /home/techspot . The complete path, that begins with a forward slash (/), to a directory is also known as its accented path. To quickly switch to the previous directory in the tree, run: cd .. , or if you want to switch to the previous working directory run cd -
four. How to view directory contents?
Use the ls command to list the contents of a directory. If the command is run without any argument, it displays the contents of the present working directory.
Here is an example:
To view the contents of any other directory, you can either specify its name (if it's a subdirectory) or its consummate path (if information technology's not a subdirectory) as an argument to the ls command.
If y'all observe closely, the output of the ls command is color coded. These different colors stand for different types of files, making it easy to visually identify them. Some of the basic colors that you should know are: Blue (Directories), White (Text files), Red (Athenaeum), Cyan (Links), Light-green (Executables), and Pinkish (Images).
5. How to view the contents of a file?
Use the cat control to view the contents of a file. This control expects a filename every bit an argument. As y'all tin come across in the screenshot below, the cat command displayed the contents of the arg.c file. However, there is a limitation. If the file is large, the output might be too big for the control line shell screen to accommodate.
In that example you tin use use the less command forth with the true cat command: cat [filename] | less . The | symbol represents a pipe, which redirects the output of the cat command to the less command, which in turn makes information technology possible for you to navigate through the file's content using the arrow keys on your keyboard to scroll up and downwards. To quit the display mode press the q cardinal.
6. How to create a new file?
Use the touch command to create a new file. The command requires a filename as statement. For example, to create a file named test.log in the present working directory, only run the command: bear upon examination.log .
To create a new file at a location other than the nowadays working directory, use the absolute path. For example, touch /dwelling/himanshu/practice/test.log.
Tip : To write anything into a newly created file, use a control line editor like Vi or Vim.
vii. How to rename/re-create/delete a file?
Apply the mv command to rename a file. For example, to rename log.txt to new_log.txt, run the command: mv log.txt new_log.txt . As always, if the file is not present in the present working directory, use the absolute path.
You tin too use the mv command to move a file from one location to other. This is the equivalent of a cutting-paste operation via GUI. For case, to motion log.txt (nowadays in current directory) to /home/himanshu, run the command: mv log.txt /home/himanshu .
To re-create a file from one directory to another, use the cp command. Similar the mv command, cp also requires a source and a destination. For example, cp log.txt /home/himanshu would create a copy of log.txt (with the same name) in the /home/himanshu directory.
To remove a file, utilise the rm command. This control expects a filename as an statement. For example, rm log.txt will remove the text file, if nowadays in the current directory, while rm /habitation/himanshu/do/log.txt will remove the text file present inside the practice directory.
To remove directories, use the -r command line pick with the rm command. For example, rm -r /home/himanshu/practice/ would delete the practice directory with all its subdirectories and files.
viii. How to search for files?
To search for files inside a given directory, use the find control. The command requires directory path and filename as statement. For example, to search for a file named inheritance.cpp in the /home/himanshu/ directory, utilize the find command in the following way:
I used sudo in the observe control above to remove certain permission errors. Yous tin skip it.
If a directory path is non specified, the find command searches in the present working directory.
You lot can too utilise wildcards with the find command to get the virtually out of it. For case, if you want to search all .c files nowadays in the /domicile/himanshu/practice directory, utilise the find command as shown below. The '*' character is a wildcard that tin can stand for whatever number of characters. For instance, tech* can correspond tech, techspot, techreport, and more than.
ix. How to search text inside files?
To search text within files, utilise the grep command. The command expects a keyword and a filename as arguments, and outputs lines that contain the keyword. For example, to search all the lines in the file /domicile/himanshu/practise/wazi/gdb/test.c that comprise the keyword ptr, utilise the grep command in the following fashion:
Utilise the -n command line option in case yous desire grep to display line numbers in output.
Tip : To search a keyword in all the files present in the current directory, utilise the * wildcard character every bit the filename.
Please notation that different the find control, the grep command doesn't search within subdirectories by default. Even so, you can enable this functionality by using the -R command line option while running the grep command.
10. What is the auto-consummate feature?
While working on the Linux command line, typing long paths, file names, and more can experience like a burden. Employ the tab key to auto complete these long names and paths easily. For instance, to write /abode, y'all can just write /ho and press tab. The command line trounce will automobile complete the proper noun for you.
In the instance in a higher place, information technology was easy for the shell to judge the proper name home considering there was no other similar candidate in the / directory. Merely in case the shell encounters similar names while motorcar completing, information technology volition display those names and you'll have to write a few more letters for the shell to know the right proper name.
Here is an example:
The shell displayed all the names that it can utilize for auto completion. If, for case, y'all wanted to write techspot , and so you'll take to type in at least c to resolve the ambiguity. In one case done, you can hit the tab central over again to autocomplete the name.
xi. What is root?
Root is the merely user that has control over the entire Linux system. It is capable of doing what normal users can't, such every bit, irresolute ownership of files, adding or removing files from system directories, and more. As you tin can guess, the root account is mostly used by system administrators simply.
The top level directory in a Linux system, represented by forwards slash (/), is known every bit root directory. It is the same directory that contains the home directory, which farther contains user specific directories. All the same, you shouldn't confuse / with the root user'due south home directory, which is located under / past the name of root .
12. What are man pages?
To acquire more than near Linux commands, yous can head over to their respective man (or Manual) pages that come up preinstalled with Linux. To open a man page, only run the man command followed by the command name. For example, run man rm to open the manual folio of the rm command. You tin can find a lot of useful information about Linux commands this way.
Next upwards, we build on the essential commands in a higher place to talk over file metadata, permissions, timestamps, as well every bit some new tools similar tee and Vim...
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 example:
Each line in the output contains metadata information related to a file or a sub-directory present in the electric current directory. This data can exist divided into the following eight parts:
+permissions that apply to the owner
|
| +permissions that apply to all other users
| |
| | +number of hard links
| | |
| | | +file size +last modification engagement/fourth dimension
_|_ _|_ | _|___ ________|__
drwx r-ten r-ten 3 himanshu himanshu 4096 Jul 3 fourteen:26 Desktop
__ ________ ________ ______
| | | |
| | | +name of file/ directory
| | |
| | +group the file belongs to
| |
| +owner of the file
|
+permissions that apply to the members of the group the file belongs to
The offset character represents the file type. For case, in the line shown above, d indicates this is a directory. Other values can be: - for normal file, s for socket file, l for link file, and more.
The next 9 characters represent permissions -- r - read, w - write, x - execute. The first set of three characters represents the owner's permission, the next 3 are group's permission, and the final 3 represent permissions granted to others who are neither the possessor, nor the part of the grouping the file belongs to. In the example shown in a higher place, the owner has read, write and execute permissions, while the group as well as others both have only read and execute permissions.
Tip: Apply the -h control line option forth with -l to display file size in human readable format.
fourteen. How to change file permissions?
Use the chmod control to alter file permissions. There are two ways in which this command can exist used. The offset method, too known as messages method, uses + , - , and = signs to add together, remove, and assign permissions. Letters a , o , u , and g represent all, others, owner, and group respectively.
For example, the chmod u=rwx somefile control assigns read, write, and execute permissions to the owner of the file somefile. Similarly, the chmod o+w somefile command adds write permission for others, the chmod yard-r somefile removes read permission from the group the file belongs to, and the chmod a+ten somefile command adds execute permission for everyone.
Specifying a is not mandatory, which means that setting permissions like +10 or -r without specifying owner, grouping or other automatically applies it to all.
The 2d method is the numbers method and it uses 4 , two , and i instead of r, westward, and x. The values are added together in sets of 3 to give us a three digit number 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 sum of numbers corresponding to r,westward, and 10. Similarly, vi represents the sum of numbers corresponding to r and w, while i represents x.
xv. How to change file timestamps?
Use the touch command to change file timestamps. There are iii types of timestamps associated with a file: Access time, Modification time, and Change fourth dimension. While the first two are self explanatory, the third 1 represents the time when the inode information or the meta data related to file last changed. Apply the stat control to display these timestamps:
To change the file admission time to the electric current time, use the bear on command with the -a option: affect -a somefile . Similarly, the -m choice changes the file modification time to the current time.
To change file timestamps to a fourth dimension other than the current time, apply the -t command line selection. For case, the control touch on -t 202207020900.01 -a somefile changes the access timestamp of somefile to 2014/07/02 09:00:01. You can also laissez passer a specific date and time in human readable form. Employ the -d command line choice for this. Here are some examples:
touch -d "2013-01-10 10:00:07" -a somefile
touch -d "next sun" -m somefile
touch -d "3 hours ago" -a somefile
16. How to determine file types?
Apply the file command to decide file types. As shown in the instance below, the control expects a filename as an argument. You can too use the wildcard * in place of file name to display the file type for every file in the current directory: file *
17. I've downloaded an executable file, only it doesn't execute, why?
In Linux (and other *nix systems) whether a file is executable or not depends solely on its permissions, not on its extension or content. When a file is downloaded, its original permissions are non known, and is hence given a default gear up of permissions that are determined by umask.
If the user actually intends to execute the downloaded file, they'll have to explicitly give executable permissions to it using the chmod command explained above. Giving permissions manually also helps foreclose virus, worms, and more from infecting your organisation without your knowledge.
18. How to print the number of new lines, words, and bytes in files?
Use the wc command to print newline, discussion, and byte counts for a file. Here is an example:
In the output shown above, v represents the number of lines, 12 represents the number of words, and 52 represents the number of bytes. You can also utilize the -l , -w , and -c command line options to separately produce number of lines, words, and bytes, respectively in the output.
19. How to display disk usage of files and directories?
Use the du command to brandish 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 important thing to note here is that the du command outputs the resident size of a file, which could be dissimilar from the actual size that the ls -50 control displays. The reason behind this difference is either slack space or thin files.
To display the combined size of a directory equally well as all its subdirectories, utilize the -s option, while -S can be used to display separate sizes. To brandish the amount of deejay space bachelor on the file system containing a specific file or directory use the df command.
Here again, the -h option is used to display the output in human readable format. If the df command is run without any file/directory name, it'll evidence disk usage for all the file systems.
20. How to compare ii files?
Utilize the unequal command to compare two files. The command 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 2 filenames as arguments, as shown in the example below.
Use the unequal command to compare these files:
Decrypting the output shown above, 5c5 means that the fifth line of somefile is changed, and should be replaced by the fifth line of the file anotherfile. The line in question from the first file is marked with a < symbol, while line from the 2nd file is marked with a > symbol.
Note - Besides c , which signifies a changed line, the unequal command besides points which lines need to be added ( a ) and deleted ( d ) for the files existence compared to match.
21. How to view the starting time few and last few lines of a file?
Utilize the head and tail commands to quickly view the start and final few lines of a file. These commands come in handy when you just want to have a quick peek within the file. For example, the caput -n2 somefile control displays the outset ii lines of the file somefile. Similarly, the tail -n3 somefile command displays the concluding iii lines of the file.
Non only lines, you can also chop-chop view a specified number of bytes using these commands. For this, employ the -c command line option instead of -n. By default, when the number of lines is not specified, both the commands display 10 lines in the output.
22. How to store and view the output of a command at once?
Employ the tee command to simultaneously write the output of any other control to standard output as well as to one or more than files. For example, the ls | tee ls-dump control displays the output of the ls command on panel and stores the output in the file ls-dump.
While the tee command is more often than not used for capturing and analyzing logs at the aforementioned time, information technology tin can also be used to speed upward your workflow. For instance, the echo "Linux command line" | tee file1 > file2 command writes the string to both files in one go.
23. How to shrink and uncompress a file?
Working on Linux requires y'all to bargain with archives similar .tar , .tar.gz , .bz2 , and more. To create likewise as uncompress these archives you lot can employ the tar control.
For example, the tar -cvf practice.tar do/ command compresses the practice folder and creates a .tar archive named practice.tar. The -c control line pick tells the tar command to create an archive, -five displays the files added to the tarball , and -f specifies the filename.
To uncompress the .tar archive created to a higher place, use the tar -xvf practice.tar command. The -10 control line option signals the command to extract the annal. This command untars the file in the electric current directory. Use the -C option to specify a unlike target directory.
To create .tar.gz and .tar.bz2 archives, add an actress -z and -j control line option, respectively. The command to uncompress these athenaeum is aforementioned as the one used for .tar files. Apply the -t command line option (along with v and f) in example you but want to listing the contents of an archive.
Tip - To deal with .zip files, employ the zip control.
24. How to edit a file using Vim editor?
While the Vim editor is i of the well-nigh powerful command line text editors, it besides requires yous to learn a lot of keyboard shortcuts. But the nuts of editing are unproblematic and piece of cake.
To open a file in the editor, run the vim control with the file name as an statement
There are 2 operation modes in Vim: control style and insert mode . The editor opens the file in control mode, where you can movement the cursor using the arrow keys on your keyboard, but cannot edit the file until you lot printing i -- activating the insert fashion as shown beneath.
Once you are done editing the file, you lot have to press the Esc primal to come out of the insert mode and into the command mode before beingness able to salve the file.
To salve the file, blazon the :w command and and so hit Enter .
To quit the editor, type the :q command and press Enter, or :wq to salvage and quit in one go.
Note - To quickly re-create 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, printing p in the control mode.
Wrap Up
While it may seem blowsy in this 24-hour interval and historic period of mod operating systems, the command line remains the almost flexible and powerful way to perform tasks in Linux. We've barely scratched the surface here, as the Linux command line has much to offering.
Each command mentioned in the article is capable of doing a lot more what we've discussed, but this should give you an overall grasp of their use. If yous get stuck somewhere, leave the states a comment or head over to our Software & Apps forum.
Note: This feature was originally published in June 2022. We have revised and bumped it because it's as relevant today as it was before if you are learning the Linux ropes. Function of our #ThrowbackThursday initiative.
Source: https://www.techspot.com/guides/835-linux-command-line-basics/
Posted by: barrettthadjuse.blogspot.com
0 Response to "A Beginner's Guide to the Linux Command Line"
Post a Comment