|
file – Verifying file type
Before using the commands on this page, you want to make sure the files you are working with are text files. To do this, use the command file. If the file you select is not a text file the commands will not work correctly and can mess up your display screen.
Example: file /etc/passwd will respond back with ASCII or Text file.
| – Pipe Utility
Many of the following commands will display more than one screenful of information. This makes it impossible to read the beginning of the displayed data. The less command automatically displays data one page at a time. By using the pipe command you send the output to less to control screen paging. To pipe the results of a command to another command, the vertical bar | character is used.
Example: sort /etc/passwd | less Will sort the file ‘/etc/passwd’ and display the results one screen at a time using the command ‘less’
top of page
Displaying Files
less
To display a text file, use the less command.
Example: less /etc/passwd
Once you have the text displayed, there are several other options you can use to move forward and backward and to search for patterns.
Command | What it does |
---|---|
page down, spacebar, or f | Scroll forward one page |
page up or b | Scroll backward one page |
down arrow, RETURN or ENTER | Down one line |
up arrow, y or k | Up one line |
G | Jump to last line of file |
g | Jump to first line of file |
/<pattern> | Search forward to find matching text. Example: /root – Will find next occurrence of root |
?<pattern> | Search backward to find matching text |
n | Search in same direction as last search for matching text |
N | Search in opposite direction of last search for matching text |
h | display list of commands to use |
q | Exit |
cat
To display the entire file without having to press RETURN for each page use the cat command.
Example: cat /etc/passwd Will display the entire contents of the file /etc/passwd.
cat can also be used to create a file.
Example: cat >newfile Will create a new file or replace the existing file ‘newfile’. After the command, enter the text for each line. To close the file, use control-D.
head
To only display the very beginning of a file, use the command head.
Example: head -5 /etc/passwd Will display the first five lines of the file /etc/passwd.
tail
To only display the very end of a file, use the command tail.
Example: tail -5 /etc/passwd Will display the last five lines of the file /etc/passwd.
top of page
grep – Finding specific content within a file
The grep utility is very useful in finding specific data within a file or group of files. Grep has many options, of which only a few are listed here.
Options | What it does |
---|---|
-c | Display count of matching lines vs displaying the actual matching text |
-v | Makes grep work the opposite. Instead of matching, list all lines that don’t match |
-i | Ignore case, search for both upper and lower case |
pattern | Text value to use for search |
filename | What files to search |
Examples | What it does |
---|---|
grep home /etc/passwd | Display all lines containing home in the file /etc/password |
grep -v home /etc/passwd | Display all lines not containing home |
grep -c home /etc/passwd | Display a count of the number of lines that contain home |
grep -c -v home /etc/passwd | Display a count of the number of lines that don’t contain ‘home’ |
grep ‘name*’ /etc/sendmail.cf | Display all lines where there is a word starting with name, such as name or names |
grep ‘domain name’ /etc/sendmail.cf | Display all lines containing domain name |
grep tcp /etc/rc* | Display all lines that contain lower case tcp for files that start with rc in the /etc directory |
grep TCP /etc/rc* | Display all lines that contain upper case TCP for files that start with rc in the /etc directory |
grep -i tcp /etc/rc* | Display all lines that contain both upper and lower case tcp for files that start with rc in the /etc directory |
Remember that you can use the pipe command to control the output from the above commands to only display the data one page at a time. To any of the above commands, add | less to the end of the command.
sort – Sorting files
Sometimes it is convenient to use the command sort on an existing file. Here a few useful options for sort.
Options:
- -r – Sort in reverse order
- <source filename>
- -o <destination file> (If this option is not used, results are displayed on screen.)
Examples | What it does |
---|---|
sort /etc/passwd | Displays the ‘/etc/passwd’ file in alphabetical order |
grep home /etc/passwd | sort | Finds lines containing ‘home’ and list results in sorted order |
grep home /etc/passwd | sort -r | Finds lines containing ‘home’ and lists in reverse order |
sort /etc/passwd -o myfile | Sorts ‘/etc/passwd’ and puts the results in the file ‘myfile’ |