Detailed UNIX Commands
Detailed UNIX Commands
The first optional parameter indicates who – this can be (u)ser, (g)roup,
(o)thers or (a)ll
The second optional parameter indicates opcode – this can be for adding
(+), removing (-) or assigning (=) permission.
The third optional parameter indicates the mode – this can be (r)ead,
(w)rite, or e(x)ecute.
Example: Add write permission for user, group and others for file1
$ ls -l
$ ls -l
The mode is a combination of three digits – the first digit indicates the
permission for the user, the second digit for the group, and the third digit
for others.
Each digit is computed by adding the associated permissions. Read
permission is ‘4’, write permission is ‘2’ and execute permission is ‘1’.
Example: Give read/write/execute permission to the user, read/execute
permission to the group, and execute permission to others.
$ ls -l
$ ls -l
While creating a new file, Unix sets the default file permissions. Unix uses the
value stored in a variable called umask to decide the default permissions. The
umask value tells Unix which of the three sets of permissions need to be
disabled.
The flag consists of three octal digits, each representing the permissions masks
for the user, the group, and others. The default permissions are determined by
subtracting the umask value from ‘777’ for directories and ‘666’ for files. The
default value of the umask is ‘022’.
List all files found in the current directory and its hierarchy
$ find.
List all files found in the current hierarchy, and all the hierarchy below
/home/xyz
$ find. /home/XYZ
Search for a file by the name abc in the current directory and its hierarchy
$ find ./ -name abc
Search for a directory by the name xyz in the current directory and its
hierarchy
$ find ./ -type d -name xyz
Search for a file by the name abc.txt below the current directory, and
prompt the user to delete each match.
Note that the “{}” string is substituted by the actual file name while running and
that the “\;” string is used to terminate the command to be executed.
Search for files that were modified in the last 7 days below the current
directory
$ find ./ -mtime -7
Search for files that have all permissions set in the current hierarchy
$ find ./ -perm 777
#1) Anchor Characters: ‘^’ and ‘$’ at the beginning and end of the pattern are
used to anchor the pattern to the start of the line, and to the end of the line
respectively.
Example: “^Name” matches all lines that start with the string “Name”. The strings
“\<” and “\>” are used to anchor the pattern to the start and end of a word
respectively.
#2) Wildcard Character: ‘.’ Is used to match any character.
Example:“^.$” will match all lines with any single character.
#3) Escaped Characters: Any of the special characters can be matched as a
regular character by escaping them with a ‘\’.
Example: “\$\*” will match the lines that contain the string “$*”
#4) Character Range: A set of characters enclosed in a ‘[‘ and ‘]’ pair specify a
range of characters to be matched.
Example: “[aeiou]” will match all lines that contain a vowel. A hyphen can be
used while specifying a range to shorten a set of consecutive
characters. E.g. “[0-9]” will match all lines that contain a digit. A carat can be
used at the beginning of the range to specify a negative range. E.g. “[^xyz]” will
match all lines that do not contain x, y or z.
#5) Repetition Modifier: A ‘*’ after a character or group of characters is used to
allow matching zero or more instances of the preceding pattern.
The grep command supports a number of options for additional controls on
the matching:
-i: performs a case-insensitive search.
-n: displays the lines containing the pattern along with the line numbers.
-v: displays the lines not containing the specified pattern.
-c: displays the count of the matching patterns.
Examples:
Match all lines that start with ‘hello’. E.g: “hello there”
$ grep “^hello” file1
Match all lines that contain any of the letters ‘a’, ‘b’, ‘c’, ‘d’ or ‘e’.
$ grep “[a-e]” file1
Match all lines that start with a digit following zero or more spaces. E.g: “
1.” or “2.”
$ grep “ *[0-9]” file1
Match all lines that contain the word hello in upper-case or lower-case
$ grep -i “hello”
Cut Command in Unix with Examples
The cut command extracts a given number of characters or columns from a file.
For cutting a certain number of columns it is important to specify the delimiter. A
delimiter specifies how the columns are separated in a text file
The cut command supports a number of options for processing different record
formats. For fixed width fields, the -c option is used.
For delimiter separated fields, the -d option is used. The default delimiter is the
tab character.
This command will extract the second and sixth field from each line, using the ‘,’
character as the delimiter.
Example:
Assume the contents of the data.txt file is:
Employee_id;Employee_name;Department_name;Salary
10001;Employee1;Electrical;20000
10002; Employee2; Mechanical;30000
10003;Employee3;Electrical;25000
10004; Employee4; Civil;40000
1
2
Employee1
Employee2
Employee3
Employee4
10001;Employee1;Electrical
10003;Employee3;Electrical
ls Syntax:
ls [options] [paths]
E.g:
dir1 dir2 file1 file2
List all the files including hidden files in the current directory
$ ls -a
E.g:
.. ... .... .hfile dir1 dir2 file1 file2
List all the files including hidden files in the current directory
$ ls -al
E.g:
total 24
List all the files in the current directory in long format, sorted by
modification time, oldest first
$ ls -lrt
E.g:
total 16
List all the files in the current directory in long format, sorted by size,
smallest first
$ ls -lrS
E.g:
total 16
E.g:
dir1 dir2 file1 file2
./dir1:
file3
./dir2:
Tar Syntax:
tar [function] [options] [paths]
Tar options:
The tar command supports the following functions:
tar -c: Create a new archive.
tar -A: Append a tar file to another archive.
tar -r: Append a file to an archive.
tar -u: Update files in an archive if the one in the filesystem is newer.
tar -d: Find the diff between an archive and the filesystem.
tar -t: List the contents of an archive.
tar -x: Extract the contents of an archive.
While specifying the function, the ‘-‘ prefix is not required, and the function can be
followed by other single letter options.
Create an archive file containing the directory tree below dir and compress
it using gzip
$ tar czvf archive.tar.gz dir
Example $ ps -ef
Example $ top
Example $ xterm
Ctrl-Z
$ bg
Continue running a job that was previously suspended (using Ctrl-Z) in the backgroun
Example $ xterm
Ctrl-Z
$ bg
$ fg
Example $ clear
Example $ history