LINUX COMMAND
man: man command in Linux is used to display the user manual of
any command that we can run on the terminal. It provides a detailed
view of the command which includes NAME, SYNOPSIS,
DESCRIPTION, OPTIONS, EXIT STATUS, RETURN VALUES,
ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS and SEE
ALSO.
whatis
Command : whatis ls / whatis date
This command gives a one line description about the command. It can be used as a
quick reference for any command.
id
Command: id / id (root) by passing username as argument
This command prints user and groups (UID and GID) of the current user.
UID : User Identifier
A UID (user identifier) is a number assigned by Linux to each user on the system. This
number is used to identify the user to the system and to determine which system resources
the user can access.
● UID 0 (zero) is reserved for the root.
● UIDs 1–99 are reserved for other predefined accounts.
● UID 100–999 are reserved by system for administrative and system
accounts/groups.
● UID 1000–10000 are occupied by applications account.
● UID 10000+ are used for user accounts.
GID : Group Id
Groups in Linux are defined by GIDs (group IDs).
● GID 0 (zero) is reserved for the root group.
● GID 1–99 are reserved for the system and application use.
● GID 100+ allocated for the user’s group.
ps
The ps command lists running processes. Using ps without any
options causes it to list the processes running in the current shell.
Command : ps
history
The history command lists the commands you have previously
issued on the command line. You can repeat any of the commands
from your history by typing an exclamation point ! and the number
of the command from the history list
Command : history
mkdir
The mkdir command allows you to create new directories in the filesystem. You must
provide the name of the new directory to mkdir. If the new directory is not going to be
within the current directory, you must provide the path to the new directory.
Command : mkdir os / mkdir os cpp ds
-v 0r --verbose
It displays a message for every directory created.
Command: mkdir -v 1 2 3
-p
A flag which enables the command to create parent directories as necessary. If the
directories exist, no error is specified.
Command: mkdir -p dss/ds/d
To create files
Create a file with touch command
We will use touch command with any extension to create file, this command will
create an empty file test.txt in your current directory
Command : touch file.txt
Create a file with cat command
cat command to create file, this command will create an empty file in your current
directory, but you must add text in the file.
Command: cat > testcat.txt
This file has been created with cat command
To save the file hit Ctrl + d and to check whether it is present in your directory or not
Command : ls -l testcat.txt
Create a file with echo command
echo command to create file, this command will create a file in the current directory but
you should add text in the line command.
Command : echo "check echo" > testecho.txt
To see the file,type command below.
Command: ls -l testecho.txt
Create a file with printf command
We will use printf command to create file, this command will create a file printf.txt in your
current directory as an example below, but you should add text in the line command.
Command: printf "This file has been created with printf command" > printf.txt
To see the file type command below.
Command: ls -l printf.txt
Create a file using nano
To create a file using nano text editor, first install it, after that type command below and
the text editor will be opened to adding text.
Command : nano nano.txt
Add the text below.
This file has been created with nano text editor
To save the file type Ctrl + x and type y, to see the file type command below.
Command: ls -l nano.txt
Create a file with vi editor
To create a file using vi text editor, type command below and the text editor will open the
file, but you can't add any text before converting it to insert mode by typing i character.
Command: vi vi.txt
Add the text below.
This file has been created with vi text editor
To save the file and exit hit Esc after that :wq, To see the file type command below.
Command : ls -l vi.txt
Create a file with vim text editor
To create a file using vim text editor, type command below and the text editor will open
the file, but you can't add any text before converting it to insert mode by typing i character.
Command : vim vim.txt
Add the text below.
This file has been created with vim text editor
To save the file and exit hit Esc after that :wq, to see the file type command below.
Command : ls -l vim.txt
How to remove a file
To remove (or delete) a file in Linux from the command line, use either the rm (remove)
or unlink command.
To delete a single file, use the rm or unlink command followed by the file name:
Command : unlink filename / rm filename
To delete a file
To delete multiple files at once, use the rm command followed by the file names separated
by space.
Command :rm filename1 filename2 filename3
You can also use a wildcard (*) and regular expansions to match
multiple files. For example, to remove all .pdf files in the current
directory, use the following command:
Command : rm *.pdf / rm *.txt
● Use the rm with the -i option to confirm each file before deleting it:
Command : rm -i filename(s)
● To remove files without prompting even if the files are write-protected pass the -f
(force) option to the rm command:
Command : rm -f filename(s)
Delete a empty directory
use ‘-d‘ option in rm command to delete a empty directory.
Command : rm -d appdata/
You can also combine rm options. For example, to remove all .txt files in the current
directory without a prompt in verbose mode, use the following command:
Command: rm -fv *.txt
How to remove directory
In Linux, you can remove/delete directories with the rmdir and rm.
rmdir is a command-line utility for deleting empty directories while with rm you can
remove directories and their contents recursively.
To remove an empty directory, use either rmdir or rm -d followed by the directory name:
Command: rm -d dirname
rmdir dirname
-r‘ option in rm command will delete all the files and sub-directories recursively of
the parent directory.
rm -r dirname
Delete the files and sub-directories interactively.
Use ‘-ri‘ option in rm command to delete file and sub-directories interactively,
Rm -ri dbstore
To remove non-empty directories and all the files without being prompted, use rm
with the -r (recursive) and -f options:rm -rf dirname
To remove multiple directories at once, use the rm -r command followed by the
directory names separated by space.
Command : rm -r dirname1 dirname2 dirname3
Delete large number files using rm
command
If your are trying to delete large number of files using rm command then you will get
an error message ‘Argument list too long’
rm *.log
Copy command
Cp command is use to copy a file from source to destination
Command :Cp target_file to source file
Copy a file interactively
If you wish to copy the files from one place to another interactively then use the “-i”
option in cp command, interactive option only works if the destination directory
already has the same file, example is shown below,
Command : cp -i filename
Copy a directory or folder (-r 0r -R)
To copy a directory from one place to another use -r or -R option in cp command. Let’s
assume we want to copy the home directory of linuxtechi user to “/mn/backup”,
Command : cp -r /home/linuxtechi /mnt/backup/
Do not overwrite the existing file while copying (-n)
cp -n /etc/passwd /mnt/backup/
Mv command
move a file
To move a file using the mv command pass the name of the file and then the new name for
the file. In the following example the file foo.txt is renamed to bar.txt.
mv foo.txt bar.txt
Move a file into a directory
To move a file into a directory using the mv command pass the name of the file and then
the directory. In the following example the file foo.txt is moved into the directory bar.
mv foo.txt bar
How to move multiple file into a directory
To move multiple files using the mv command pass the names of the files or a pattern
followed by the destination.
mv file1.txt file.2.txt file3.txt folder
The following example is the same as above but uses pattern matching to move all files
with a .txt extension.
mv *.txt folder
Move a directory
To move a directory using the mv command pass the name of the directory to move
followed by the destination.
mv foo bar
How to prompt before overwriting a file
mv -i foo.txt bar.txt
How to not overwrite an existing file
To prevent an existing file from being overwritten pass the -n option
mv -n foo.txt bar.txt
How to take a backup of an existing file
To take a backup of an existing file that will be overwritten as a result of the mv
command pass the -b option. This will create a backup file with the tilde character
appended to it.
mv -b foo.txt bar.txt
grep
The grep utility searches for lines which contain a search pattern. When we looked at the
alias command, we used grep to search through the output of another program, ps . The
grep command can also search the contents of files.
grep train *.txt
gzip
The gzip command compresses files. By default, it removes the original file and leaves
you with the compressed version. To retain both the original and the compressed
version, use the -k (keep) option.
gzip -k core.c
head
head command gives you a listing of the first 10 lines of a file. If
you want to see fewer or more lines, use the -n (number) option. In
this example, we use head with its default of 10 lines. We then
repeat the command asking for only five lines.
Head -n 5 test.txt
tail
The tail command gives you a listing of the last 10 lines of a file. If you want to see
fewer or more lines, use the -n (number) option. In this example, we use tail with its
default of 10 lines. We then repeat the command asking for only five lines.
tail core.c
tail -n 5 core.c
less
The less command allows you to view files without opening an editor. It’s faster to use,
and there’s no chance of you inadvertently modifying the file. With less you can scroll
forward and backward through the file using the Up and Down Arrow keys, the PgUp and
PgDn keys and the Home and End keys. Press the Q key to quit from less.
To view a file provide its name to less as follows:
less core.c
ssh
Use the ssh command to make a connection to a remote Linux
computer and log into your account. To make a connection, you
must provide your user name and the IP address or domain name of
the remote computer. In this example, the user mary is logging into
the computer at 192.168.4.23.
ssh mary@192.168.4.23
Cd command
The cd command is used to change the current directory (i.e., the directory in which the
user is currently working) in Linux
Navigate to the Previous Directory
To change back to the previous working directory, pass the dash (-) character as an argument to the cd
command:
cd -
Navigate to the Home Directory
To navigate to your home directory simply type cd. Another way to return directly to your
home directory is to use the tilde (~) character, as shown below:
cd ~
Ls command
Ls command shows the list of files and directories , where you won’t be able to view
details like file types, size, modified date and time, permission and links etc.
ls --version
1. A. ls -l shows file or directory, size, modified date and time, file or folder name
and owner of file and its permission.
1. B. View Hidden Files:
ls -a : contains the list of all the hidden file including “ .” and “. .”
1. C. List Files with Human Readable Format
ls -lh : contains all the option which can be understood by non development person.
1. D. Sort Files by Files size
ls -lS / ls -S -l: displays file size in order of bigger size first.
ls -i :
i over here is to check Inode Informations.
An inode is a data structure that stores various information about a file in Linux, such
as the access mode (read, write, execute permissions), ownership, file type, file size,
group, number of links, etc. Each inode is identified by an integer number. An inode
is assigned to a file when it is created.
ls -R :
You can list directories recursively using ls -R.
Recursively means it will list all the directory with is all subdirectory in a tree format.
ls -lt :
Linux ls command with -t will list the files and directories by it’s modification date in
ascending order, means the higher will be come first.
ls ~ : command to list the contents in the user's home directory.
ls -d */ command to list only directories:
ls * command to list the contents of the directory with it's subdirectories
ls -S (the S is uppercase) : command to list files or directories and sort by date or time in
descending order (biggest to smallest).
You can also add a -r flag to reverse the sorting order like so: ls -Sr
ls -n: ls command with option -n will show the UID (User ID) and GID (Group ID) numbers of file and directory.
A UID (user identifier) is a number assigned by Linux to each user on the system. This number is used to identify the user to the system and to
determine which system resources the user can access.
● UID 0 (zero) is reserved for the root.
● UIDs 1–99 are reserved for other predefined accounts.
● UID 100–999 are reserved by system for administrative and system accounts/groups.
● UID 1000–10000 are occupied by applications account.
● UID 10000+ are used for user accounts.
GID
Groups in Linux are defined by GIDs (group IDs).
● GID 0 (zero) is reserved for the root group.
● GID 1–99 are reserved for the system and application use.
● GID 100+ allocated for the user’s group.
ls -G
If you want to list only Owner of the files and directories to which they are belongs and not groups then use Linux ls command
with argument -G.
User: The owner of a file belongs to this class
Group: The members of the file’s group belong to this class. A group is a collection of users. The main
purpose of the groups is to define a set of privileges like read, write, or execute permission for a given
resource that can be shared among the users within the group.
Other: Any users that are not part of the user or group classes belong to this class.