Work with files
LS command
ls -F: command appends a special character at the end of each filename displayed to indicate the type of file.
- @ symbol
indicates a linked file - the * symbol
indicates an executable file, - the /
indicates a subdirectory, - the ¼ character
indicates a socket, - the | character
indicates a named pipe. - All other file
types do not have a special character appended to them and could be text files, binary data files, or special device files.
Which switch below can be added to the ls command to show a list of files and their type?
Answer: -F
ls –l command can be used to provide a long listing for each file in a certain directory.
eg.
[root@server1 ~]# ls -l
total 548
lrwxrwxrwx 1 root root 9 Apr 7 09:56 current -> project12
drwx—– 3 root root 4096 Mar 29 10:01 Desktop
-rwxr-xr-x 1 root root 519964 Apr 7 09:59 myprogram
-rwxr-xr-x 1 root root 20 Apr 7 09:58 myscript
Each file listed in the preceding example has eight components of information listed in columns from left to right:
1. A file type character
• The d character represents a directory.
• The l character represents a symbolically linked file .
• The b or c characters represent special device files.
• The n character represents a named pipe.
• The s character represents a socket.
• The – character represents all other file types (text files, binary data files).
2. A list of permissions on the file (also called the mode of the file)
3. A hard link count
4. The owner of the file
5. The group owner of the file
6. The file size
7. The most recent modification time of the file
8. The filename (Some files are shortcuts or pointers to other files and indicated with an arrow ->, as with the file called “current” in the preceding output; these are known as symbolic links
Work with text file
Cat, Tac, sort, Head, Tail, more, less
CAT: display all the text file contents.
- -n: show the line number
TAC: display the text file in the reverse order. Useful in the log file.
Sort: It sorts the contents of a text le, line by line.
• -n: Will sort as per the numeric value
• -d: Will sort as per the dictionary meaning
• -r: Will sort in the reverse order
• -t: Option to specify delimiter for fields
• +num: Specifies sorting field numbers
• -knum: Specifies sorting filed numbers
Examples of command usage
Explanation
1 sort sample.txt
Alphabetically sorting of lines
2. sort -u sample.txt
Duplicate entries are sorted
3. sort -r sample.txt
Reverse sort
4. sort-n -k3 sample.txt
Numerical sorting of the 3rd field
HEAD: display the first 10 lines by default, add -3 to display the first 3 lines and so forth.
Tail: display the last 10 lines by default, can use -number to show your wanted lines.
More: display the text file with progress.
- Spacebar: next page
- Enter: next line.
- q key: quit.
Less: same as more command but add more options.
- h: help
- d: forward half-window
- u: backward half-window
- z: forward one window
- w: backward one window
Display binary files:
If you use the cat command to display binary files, you will get some unreadable code.
Strings: display binary in strings, these characters may indicate what the binary file is used for.
od: display binary in octal format
Search for text within file:
grep: search the lines which contains regular expression. By default, it is case sensitive.
To display the line which contains the word “root” in the shadow file: grep "root" /etc/shadow
- -v: display the lines which don’t contain the text.
- -i: perform the case insensitive search.
- -R – recursively search files in subdirectories.
To find your “keyword” in multiple files under a folder, use below command:
grep -iRl "keyword" ./
AWK
searches for patterns of text and performs some action on the text found.However,the awk command treat search line of text as a record in a data base,
and each word in a line as a database field.
Synopsis:
gawk [ POSIX or GNU style options ] -fprogram-file [ — ] file …
gawk [ POSIX or GNU style options ] [ — ] program-text file …or
awk [ POSIX or GNU style options ] -fprogram-file [ — ] file …
awk [ POSIX or GNU style options ] [ — ] program-text file …
- -F: By default,the awk command uses space or tab characters as delimiters for each field in a line. Most configuration files on Linux systems, however,are delimited using colon(:)characters. To change the delimiter, use -F option.
- -f:-f program-file–file program-fileRead the AWK program source from the file program-file, instead of from the first command line argument. Multiple -f (or –file) options may be used.
For example, in the security area, to get the password hash for the root user in the shadow file, use the following command:
cat shadow | awk -F : '/root/{print $2}'
sed:
Sed is a stream editor. A stream editor is used to perform basic text transformations on an input stream (a file or input from a pipeline). While in some ways similar to an editor which permits scripted edits (such as ed), sed works by making only one pass over the input(s), and
is consequently more efficient. But it is sed’s ability to filter text in a pipeline which particularly distinguishes it from other types of editors.
Options:
-n, –quiet, –silent
suppress automatic printing of pattern space
-e script, –expression=script
add the script to the commands to be executed
-f script-file, –file=script-file
add the contents of script-file to the commands to be executed
sed can be used to replace text, which is one of it’s most popular use. Note that by default, only replace the first occurrence.
sed s/search/replace
- append g :
sed s/search/replace/g
, to replace all the occurrences in the file. - add line number:
sed 5,8s/search/replace/g
, only replace line 5,6,7,8. note that there is no space between the 8 and s.
Practice
Show the first 10 lines of the text file in 4 ways:
head command example to print first 10/20 lines
Type the following head command to display first 10 lines of a file named “bar.txt”:
head -10 bar.txtType the following head command to display first 20 lines of a file named “bar.txt”:
head -20 bar.txt
sed command example to print first 10/20 lines
Type the following sed command to display first 10 lines of a file named “/etc/group”:
sed -n 1,10p /etc/groupType the following sed command to display first 20 lines of a file named “/etc/group”:
sed -n 1,20p /etc/groupawk command example to print first 10/20 lines
Type the following awk command to display first 10 lines of a file named “/etc/passwd”:
awk 'FNR <= 10' /etc/passwdType the following awk command to display first 20 lines of a file named “/etc/passwd”:
awk 'FNR <= 20' /etc/passwdperl command example to view first 10/20 lines of a file
Type the following perl command to display first 10 lines of a file named “/etc/passwd”:
perl -ne'1..10 and print' /etc/passwdType the following perl command to display first 20 lines of a file named “/etc/passwd”:
perl -ne'1..20 and print' /etc/passwd |
Cut
The cut command
The cut command is used to extract speci ed columns/characters of a text, which is given as follows:
• -c: Will specify the filtering of characters
• -d: Will specify the delimiter for fields
• -f: Will specify the field number
From the /etc/passwd file, the fields 1 and 3 will be displayed. The display will contain the login name and user ID.
We used the –d: option to specify that the field or columns are separated by a colon (:):
$ cut -d: -f1,3 /etc/passwd
In a text file, which is fix weight, the -c switch will be useful.
To show characters 1 to 3 and 8 to 12 of each line in a file:
$ cut -c1-3,8-12