Basic Commands: Difference between revisions
No edit summary |
|||
| Line 1: | Line 1: | ||
==ls Command== | ==''ls'' Command== | ||
<code>ls</code> is one of the most frequently used basic command in Linux. The ls command-line utility is used to list files and directories that exist inside a directory. | <code>ls</code> is one of the most frequently used basic command in [[Linux]]. The ls command-line utility is used to list files and directories that exist inside a directory. | ||
By default, when used without any options, ls lists only the contents of the current directory in alphabetical order without any additional information. | By default, when used without any options, ls lists only the contents of the current directory in alphabetical order without any additional information.<syntaxhighlight lang="shell"> | ||
ls | |||
</syntaxhighlight>Type ls <code>/home/bob/Project</code> to see the contents of the directory Project. | |||
Type ls <code>/home/bob/Project</code> to see the contents of the directory Project. | |||
You can use options with the ls command for variation in results: | You can use options with the ls command for variation in results: | ||
| Line 14: | Line 12: | ||
*<code>ls -ltr</code> list file names in the last modification time in reverse order. | *<code>ls -ltr</code> list file names in the last modification time in reverse order. | ||
The output of ls -al | The output of <code>ls -al</code><syntaxhighlight lang="shell"> | ||
-rw-r--r-- 1 bob developers 281 Apr 1 2020 /source/project/learn.py | |||
</syntaxhighlight>The owner (ie bob) has read and write permissions, other members of the group have read permissions, and the rest of the world has read permissions on the file. The file is owned by the user named bob and belongs to the developers group. The total size of the file is 281 bytes. | |||
==''cd'' Command== | |||
==cd Command== | |||
<code>cd</code> stands for change directory, which is used to change the current working directory. The cd command is used to navigate through [[Linux]] files and directories. You need to specify either the full path of the target directory or the directory name you want to change to. | <code>cd</code> stands for change directory, which is used to change the current working directory. The cd command is used to navigate through [[Linux]] files and directories. You need to specify either the full path of the target directory or the directory name you want to change to. | ||
If you are in the <code>/home/bob/Documents</code> directory and need to go to the Photos subdirectory, then type: | If you are in the <code>/home/bob/Documents</code> directory and need to go to the Photos subdirectory, then type:<syntaxhighlight lang="shell"> | ||
cd Photos | |||
</syntaxhighlight>Use the absolute path to change directory, change to <code>/home/bob/Pictures</code> directory:<syntaxhighlight lang="shell"> | |||
Use the absolute path to change directory, change to <code>/home/bob/Pictures</code> directory: | cd /home/bob/Pictures | ||
</syntaxhighlight>Some shortcuts that you can use for easy navigation: | |||
Some shortcuts that you can use for easy navigation: | |||
*<code>cd ..</code> moves one directory up. | *<code>cd ..</code> moves one directory up. | ||
| Line 34: | Line 29: | ||
<code>cd -</code> switch to the parent directory | <code>cd -</code> switch to the parent directory | ||
==mkdir Command== | ==''mkdir'' Command== | ||
The mkdir stands for make directory. The mkdir command is used to create a new directory in Linux. | The <code>mkdir</code> stands for make directory. The <code>mkdir</code> command is used to create a new directory in [[Linux]]. | ||
Use <code>-p</code> option to create the entire directory structure. For example to create a directory 2020 and its sub-directory Source in the existing Documents directory, type: | When you type mkdir Pop, it will create a directory named Pop in the current directory. Whereas, the following Linux command will create a directory named Source inside the Documents directory:<syntaxhighlight lang="shell"> | ||
mkdir Documents/Source | |||
</syntaxhighlight>Use <code>-p</code> option to create the entire directory structure. For example to create a directory <code>2020</code> and its sub-directory Source in the existing <code>Documents</code> directory, type:<syntaxhighlight lang="shell"> | |||
mkdir -p Documents/2020/Source | |||
</syntaxhighlight> | |||
==cat Command== | ==''cat'' Command== | ||
The cat stands for concatenate. It is one of the basic command in the Linux operating system. The cat command is used to concatenate files, view the content of a file, create a new file, or redirect output in files. | The <code>cat</code> stands for concatenate. It is one of the basic command in the [[Linux]] operating system. The cat command is used to concatenate files, view the content of a file, create a new file, or redirect output in files. | ||
Type cat command followed by a file name to see the contents of the file. | Type cat command followed by a file name to see the contents of the file.<syntaxhighlight lang="shell"> | ||
cat users.txt | |||
</syntaxhighlight>The cat command can also be used to perform some advanced operations, such as: | |||
The cat | To create a new file named <code>file1.txt</code>, type:<syntaxhighlight lang="shell"> | ||
cat > file1.txt | |||
</syntaxhighlight>The existing contents of the <code>department.txt</code> file will be overwritten by the contents of <code>users.txt</code>:<syntaxhighlight lang="shell"> | |||
cat users.txt > department.txt | |||
</syntaxhighlight>To convert the content of file <code>users.txt</code> from lowercase to uppercase and store it in <code>output.txt</code>, use the following command:<syntaxhighlight lang="shell"> | |||
cat users.txt | tr a-z A-Z > output.txt | |||
</syntaxhighlight> | |||
==''df'' Command== | |||
==df Command== | |||
The df stands for disk filesystem. It is used to get a complete summary of used and available disk space of the file system in your [[Linux]] computer. | The df stands for disk filesystem. It is used to get a complete summary of used and available disk space of the file system in your [[Linux]] computer. | ||
Options can be used with the df command to get variations in the output such as: | Options can be used with the <code>df</code> command to get variations in the output such as: | ||
*<code>-a</code> displays all file systems. | *<code>-a</code> displays all file systems. | ||
| Line 71: | Line 64: | ||
*<code>-g</code> displays output in gibibyte (GiB). | *<code>-g</code> displays output in gibibyte (GiB). | ||
Check disk usage information of a particular file system, type: | Check disk usage information of a particular file system, type:<syntaxhighlight lang="shell"> | ||
df /dev/sda5 | |||
</syntaxhighlight>'''Output''' | |||
'''Output''' | |||
Filesystem 1K-blocks Used Available Use% Mounted on | Filesystem 1K-blocks Used Available Use% Mounted on | ||