Updated May 31, 2023
What is Unix Shell Commands?
Unix shell commands are one of the four layers of Unix architecture, enabling human interaction with the operating system to intimate it to begin certain processes by giving commands through the interpreter. It consists of a set of commands like cp, grep, cat, id, mv, etc., that are pre-defined and stored in the libraries. Unix shell commands are of three types basic, intermediate, and advanced, consisting of various commands like ls, cat, rm, mv, mkdir, chmod, find, chown, chgrp, head, tail, grep, ln, cut, etc.
UNIX Architecture
Here is the following UNIX architecture mentioned below
- Layer-1: Hardware
This layer consists of all the hardware resources being used.
- Layer-2: Kernel
The kernel is like the heart of the operating system. It is the mode of interaction between the hardware and the operating system. It also manages tasks and resources using scheduling processes for the smooth functioning of the system.
- Layer-3: Shell commands
It is how a human interacts with the operating system and tells it to start certain processes. An interpreter is used where we give the command for the operating from the set of all commands for which the definition has been defined and stored in the libraries.
Some examples of commands are cp, mv, cat, grep, id, wc, nroff, a.out, and more.
- Layer-4: Application Layer
It executes the given external applications. It is the outermost layer to execute the applications.
List of Unix Shell Commands
Here is the list of the following Unix Shell Commands mention below:
- Basic.
- Intermediate.
- Advanced.
Let us see the above List of Unix Shell Commands in detail
1. Basic Unix Shell Commands
a. Listing files (ls) – ‘ls’ command lists all the files in a directory.
Syntax-
ls -<option> directory_name
Example-
ls test
option | Description |
ls -a | To list all files with the hidden files starting with ‘.’ |
ls –color | Shows colored list which can be [=always/never/auto] |
ls -d | To list all directories |
ls -F | To add one char to the entries |
ls -i | To list all files ignoring the case |
ls -l | To list all the details of the file |
ls -la | list long format, including hidden files |
ls -lh | list long format with the readable file size |
ls -ls | list with the long format with the file size |
ls -r | list in reverse order |
ls -R | list directory tree recursively |
ls -s | list file size |
ls -S | sort by file size |
ls -t | sort by time & date |
ls -X | sort by extension name |
b. Creating & Viewing Files – ‘cat command can be used to create the file or view the contents of the file.
Syntax –
cat >filename
Example – cat > test1.dat – will create a file and wait for the input to be written into the file.
cat filname – will display the contents of the file on the screen.
c. Deleting Files -‘ rm’ command deletes a file from the directory.
Syntax-
rm filename
Tag | Description |
-f, –force | ignore nonexistent files, never prompt. |
-i | prompt before every removal. |
d. Moving and Re-naming files – ‘mv’ command is used for moving a file from one location to another. This command can also be used to rename the file as the source file gets deleted and a new file is created.
Syntax–
mv <source_file> <target_file>
Example – mv test1.dat test2.dat – here, contents of the test1 file get copied to test2.dat in the same directory, and the test1.dat file gets deleted.
e. Making directories – Unix also provides us with the command to make our own directory. It is just like making our own folder where all relevant files can be stored.
Syntax –
mkdir <dir_name>
Example – mkdir /abi/sand/results – this command will create a directory at /abi/sand path . This command will not work if /abi/sand/ path does not exist.
2. Intermediate
a. Chmod – Sometimes, when we write into a write-protected file, we need to change the permissions given to a file or directory. Here ‘chmod’ command is used to give suitable permissions. But one should know the pattern for giving permissions.
Permissions are given as rwxrwxrwx
We must set permission to 1 if we need to enable it and 0 if it needs to be disabled.
For instance, if one wants to only read and execute permissions to users and others but all permissions to the group. Then we must set it as ‘101111101’. And that means ‘575’ if converted to decimal in triplets. Thus for giving permissions, we give the command as
Example –
chmod 575 file1.dat
b. Find – This command finds the files or directories and subdirectories in a particular directory.
Syntax –
find <options> <paths>
Example –
Option | Description |
-atime n | Returns true if the file was accessed n days ago |
-ctime n | Returns true if the file was changed n days ago |
-mtime | Returns true if file contents were modified n days ago |
-name | Return true if filename matching a particular pattern |
-size | Returns true if the file size is n blocks. |
-type c | Returns true if the file being searched is of type c( if c = ‘f’ means it is a file; if it ‘d’, means it is a directory) |
Example – If someone wants to search for file names ‘test1’ in the directory, he should give a command like –
find –type f –name test1 /abi/sand
– This command will give all test1 file in /abi/sand directory
c. chown – change ownership of the file. Sometimes someone wants to change the file owner so that someone working on that file can access that file. Only the file owner has the right to change the file ownership.
Syntax:
chown [owner] [file]
Example: Change the owner of test1 to user name ‘aaggasa’, assuming that the current user currently owns it
> chown aaggasa test1
d. chgrp: change the group ownership of the file. This command is used to change the group to which the file belongs. Only the file owner has the right to change the file ownership.
Syntax:
chgrp [group] [file]
Example: Change the group of test1 to group2, assuming the current user currently owns it.
> chgrp group2 test1
e. Head: Unix gives us this command-line utility to extract the first part of the file. It writes the result on standard output.
Syntax –
head <option> <filename>
Option | Description |
-n | Used to specify the number of lines to be fetched |
–c | Used to specify the number of bytes to be fetched. |
-q | Used to suppress the header line. |
Example – If someone wants to extract the first 5 lines of the file, we must use
>head –n 5 /abi/sand/test1.dat
Note – By default, UNIX will show 10 lines in case no option is specified with the head command.
f. Tail: Unix gives us this command-line utility to extract the first part of the file. It writes the result on standard output.
Syntax –
tail <option> <filename>
Option | Description |
-n | Used to specify the number of lines to be fetched |
–c | Used to specify the number of bytes to be fetched. |
-q | Used to suppress the header line. |
Example – If someone wants to extract the first 5 lines of the file, we must use
>head –n 5 /abi/sand/test1.dat
Note – By default, UNIX will show 10 lines in case no option is specified with the head command.
3. Advanced Unix Shell Commands
a. Grep: This command utility helps search a particular pattern or character in the file. It returns all the lines that match the pattern in that particular file.
Syntax-
grep <options> <pattern> <files>
Option | Description |
-n | Display the matched lines and their line numbers. |
-v | To print the lines that do not match the pattern. |
-l | To display the list of filenames. |
-c | The count of lines that matches the pattern can be extracted. |
-h | Display the matched lines, but do not display the filename |
-i | Ignores, the case for matching |
-w | To Match the whole word in the expression |
b. ln: make links and symlinks to files and directories. A symbolic link comprises a special type of file containing a reference to another file. This helps to create a link between files. There are 2 types of links”-
- Soft link – It refers to the abstract path to a file.
- Hard Link – It refers to the exact location of that file,
To create a soft link ‘ln’ command is used.
Syntax –
ln -s {source_filename} {symbolic_filename}
Example – If we want to create a softlink link l1 to the path ‘/abi/sand/dir1’, then we must execute the following command:-
>ln link1 /abi/sand/dir1
This command will create a link to the directory in the current directory
To check the link execute-
ls –l
Output – lrwxrwxrwx 1 priya priya 16 2007-09-25 22:53 link1 -> /abi/sand/dir1
c. cut – This command utility extracts a particular column from a file. To extract a column, we need to specify the delimiter to help distinguish the columns in that file.
Syntax–
cut <options> <file>
Option | Description |
-c | For fixed-width fields, the -c option is used. |
-d | For specifying the delimiter. By default, the delimiter is a tab. |
-b | For specifying the number of bytes to be extracted |
-f | For specifying the field number that needs to be extracted. |
Example – If someone wants to extract the second field from the ‘city.txt’ file where ‘|’ is treated as a delimiter for the columns.
cut –d "|" –f 2 city.txt
Conclusion
Unix Commands are a powerful tool that helps users execute the processes and do various tasks they want. Its inbuilt parser helps in development using various scripting languages. With its powerful set of commands utility, one can perform all the features that need to be read from registers.
Recommended Articles
We hope that this EDUCBA information on “Unix Shell Commands” was beneficial to you. You can view EDUCBA’s recommended articles for more information.