Common CLI commands and file utilities
man
command to manuals
In Linux, the man command is used to view the manual page for a command. A manual page (or “man page”) is a detailed documentation for a command that provides information about the command’s syntax, options, and examples of usage.
To use the man command, you simply need to specify the name of the command that you want to view the manual page for as an argument. For example, to view the manual page for the ls command, you would enter:
man ls
This will display the manual page for the ls command in a paginated format, with each page displaying a few lines at a time. You can use the space bar to move to the next page, and the q key to exit the manual page and return to the command prompt.
You can also use the -k option with man to search for manual pages that contain a specific keyword. For example:
man -k search
This will display a list of manual pages that contain the keyword “search” in the title or description.
To learn more about the man command and see a list of the available options, you can use the man man command to view the manual page for man, or you can use the man –help command to view a summary of the available options.
cat
command to concatenate and show text files
In Linux, the cat command is used to concatenate and display the contents of one or more files. It can be used to view the contents of a file, or to create a new file by combining multiple files.
To use the cat command, you simply need to specify the name of the file or files that you want to view as arguments. For example, to view the contents of a file called myfile.txt, you would enter:
cat myfile.txt
The cat command will display the contents of the file to the standard output, which is usually the terminal window.
You can also use the cat command to create a new file by combining multiple files. To do this, you can specify the names of the source files as arguments, followed by the name of the destination file as the last argument. For example:
cat file1.txt file2.txt > newfile.txt
This will create a new file called newfile.txt that contains the contents of file1.txt and file2.txt concatenated together.
more
command to display the context one screen at a time
In Linux, the more command is used to display the contents of a file one screen at a time. It is often used to view large files that do not fit on a single screen.
To use the more command, you simply need to specify the name of the file that you want to view as an argument. For example, to view the contents of a file called myfile.txt, you would enter:
more myfile.txt
The more command will display the contents of the file one screen at a time, with a prompt at the bottom of the screen indicating how many lines are left in the file. You can use the space bar to move to the next screen, and the q key to exit the more command and return to the command prompt.
less
command view and navigate through a file content
less is unix-like operating systems allows you to view the contents of a file or command output one page at a time. This can be useful if the file or output is very long and would otherwise be difficult to read or navigate on the terminal.
To use less, you can simply type less followed by the name of the file you want to view:
less file.txt
You can also use less to view the output of a command by preceding the command with a | (pipe) symbol:
command | less
Once the file or output is displayed in less, you can use the following commands to navigate and interact with the text:
Space: Display the next page of text
b: Display the previous page of text
/pattern: Search for the specified pattern
n: Repeat the last search
q: Quit less and return to the terminal
You can also use the more command to view the output of other commands. For example, you can use the | (pipe) operator to redirect the output of a command to more, like this:
ls -l | more
This will display the output of the ls -l command one screen at a time, allowing you to scroll through the output.
touch
command in file system
In Linux, the touch command is used to create a new empty file or update the timestamp of an existing file. To use the touch command, you simply need to specify the name of the file that you want to create or update as an argument. For example, to create a new empty file called myfile.txt, you would enter:
touch myfile.txt
By default, the touch command will create the new file in the current working directory. You can also specify a different directory path as an argument to create the new file in a different location. For example:
touch /home/john/myfile.txt
This would create the myfile.txt file in the /home/john directory.
If the file specified as an argument already exists, the touch command will update the timestamp of the file to the current time. This can be useful for updating the timestamp of a file without modifying its contents.
file
command to get the type of files
The file command is a utility in Unix-like operating systems that is used to determine the type of a file. It examines the contents of a file and attempts to classify it based on known file types, such as whether it is an ASCII text file, a binary executable, or a JPEG image.
To use file, you can type file followed by the name of the file you want to classify:
file file.txt
This will display the file type of file.txt, for example:
file.txt: ASCII text
You can also use file to classify multiple files at once by listing the filenames separated by spaces:
file file1.txt file2.jpg file3.bin
find
command to search for files in file system
In Linux, the find command is used to search for files and directories based on various criteria, such as name, size, and type. To use the find command, you need to specify the directory where you want to start the search and any search criteria as arguments.
For example, to search for all files in the current working directory with the name myfile.txt, you would enter:
find . -name myfile.txt
The . specifies the current working directory as the starting point for the search, and the -name option specifies the name of the file to search for.
You can also use the -type option to specify the type of file to search for, such as f for regular files or d for directories. For example, to search for all directories in the /usr directory, you could enter:
find /usr -type d
You can use the -size option to search for files based on their size. For example, to search for all files larger than 100KB in the current working directory, you could enter:
find . -size +100KB
You can use multiple options together to specify more complex search criteria. For example, to search for all regular files with the .txt extension in the /usr directory that are larger than 50KB, you could enter:
find /usr -type f -name "*.txt" -size +50KB
date
command to get and set date
The date command is a utility in Unix-like operating systems that is used to display or set the current date and time on a Linux system. When run with no options, date displays the current date and time in the default format.
Here are some examples of using the date command:
Displaying the current date and time:
To display the current date and time, simply type date:
date
This will display the date and time in the default format, which is typically something like ‘Fri Dec 17 14:59:01 EST 2021’.
Displaying the date in a specific format:
You can use the + option to specify a custom format for the date and time. The format is specified using strftime format strings, which are preceded by a % symbol.
For example, to display the date and time in the format YYYY-MM-DD HH:MM:SS, you can use the following command:
date +"%Y-%m-%d %H:%M:%S"
This will display the date and time in the format ‘2021-12-17 14:59:01’.
Setting the date and time: You can use the –set option to set the date and time to a specific value. You must specify the date and time in the format YYYY-MM-DD HH:MM:SS, and you must have superuser privileges (i.e., you must be root) to use this option.
sudo date --set="2021-12-17 14:59:01"
Displaying the current time in a different time zone: You can use the -d option to specify a time zone for the date and time to be displayed in. This can be useful if you want to see the current time in a different time zone.
For example, to display the current time in the Pacific time zone (PST), you can use the following command:
date -d 'PST'
Increasing or decreasing the date and time: You can use the –date option to increase or decrease the date and time by a certain amount. This can be useful if you want to see what the date and time will be in the future or the past.
For example, to display the date and time one day in the future, you can use the following command:
date --date='+1 day'
To display the date and time one week in the past, you can use the following command:
date --date='-1 week'
diff
command to compare two files
The diff command is a utility in Unix-like operating systems that is used to compare the contents of two files or directories and display the differences between them. When run with two filenames as arguments, diff will display the differences between the two files in a format that shows which lines have been added, deleted, or changed.
Here is an example of using diff to compare two text files:
diff file1.txt file2.txt
The output of diff will show the differences between the two files, with lines that are present in only one of the files indicated by a < or > symbol. For example:
1c1
< This line is present in file1.txt but not in file2.txt
---
> This line is present in file2.txt but not in file1.txt
You can use various options to control the output format or to specify how the comparison is performed. For example, the -u option will display the output in a unified diff format, which includes the context of each change:
diff -u file1.txt file2.txt
You can also use diff to compare directories by specifying the names of the directories as arguments:
diff dir1 dir2
This will compare the files and directories in dir1 and dir2 and display the differences between them.
Here are some advanced examples of using the diff command in Linux:
Ignoring leading or trailing white space: You can use the -b option to ignore leading or trailing white space when comparing files. This can be useful if the files contain changes that are only whitespace changes, and you want to ignore those changes.
diff -b file1.txt file2.txt
Ignoring changes in case: You can use the -i option to ignore changes in case when comparing files. This can be useful if the files contain changes that only involve the case of characters, and you want to ignore those changes.
diff -i file1.txt file2.txt
Ignoring changes to blank lines: You can use the -B option to ignore changes to blank lines when comparing files. This can be useful if the files contain changes that only involve the addition or deletion of blank lines, and you want to ignore those changes.
diff -B file1.txt file2.txt
Comparing files with different line endings: You can use the –strip-trailing-cr option to ignore changes to the line endings when comparing files. This can be useful if the files have different line endings (e.g., one uses Unix-style LF line endings and the other uses Windows-style CRLF line endings) and you want to ignore those differences.
diff --strip-trailing-cr file1.txt file2.txt
Comparing directories recursively: You can use the -r option to compare directories recursively, which means that diff will compare all subdirectories and files within the directories.
diff -r dir1 dir2
Saving the output to a file: You can save the output of diff to a file by using the > operator to redirect the output to a file.
diff file1.txt file2.txt > differences.txt
For more information on diff and its options, you can consult the diff man page (man diff).
wc
command to display lines, words and bytes in a file
The wc (word count) command is a utility in Unix-like operating systems that is used to display the number of lines, words, and bytes in a file or the output of a command. When run with a filename as an argument, wc will display the line, word, and byte counts for the file.
Here is an example of using wc to count the number of lines, words, and bytes in a text file:
wc file.txt
The output of wc will show the line, word, and byte counts for the file, in that order. For example:
10 35 222 file.txt
This means that the file file.txt has 10 lines, 35 words, and 222 bytes.
You can use various options to control which counts are displayed or to modify the way the counts are calculated. For example, the -l option will only display the line count, the -w option will only display the word count, and the -c option will only display the byte count.
Here is an example of using wc to display only the word count for a file:
wc -w file.txt
You can also use wc to count the number of lines, words, and bytes in the output of a command by preceding the command with a | (pipe) symbol:
command | wc
For more information on wc and its options, you can consult the wc man page (man wc).
Summary:
Command | Description |
man |
Displays manual page for a command |
cat |
Concatenates and displays contents of one or more files |
more |
Displays contents of a file one screen at a time |
less |
Allows scrolling through contents of a file with up and down arrow keys |
touch |
Creates a new empty file or updates timestamp of existing file |
file |
Determines type of a file based on its contents |
find |
Searches for files and directories based on various criteria |
date |
Displays or sets current date and time |
diff |
Compares contents of two files or directories and displays differences |
wc |
Displays number of lines, words, and bytes in a file or output of a command |