LINUX COMMANDS
(CONTD..)
ls
• For listing directory contents(a list of all filenames in the current directory)
• List of filenames arranged in ASCII collating sequences(number first, uppercase,and
then lowercase)
• ls calender (here calender is filename)
To check whether a particular file is available or not.
• ls –x (produce output in multiple columns)
• ls –F
• (For identifying Directories and Executables)
• -F classifies the file with different special characters for different kinds of files.
/ – directory.
nothing – normal file.
@ – link file.
* – Executable file
• ls –a
• To show all the hidden files in the directory, use ‘-a option’. Hidden files in Unix starts
with ‘.’ in its file name. It will show all the files including the ‘.’ (current directory) and
‘..’ (parent directory).
• ls –axF
• For listing directory contents
• ls –x helpdir progs
• Recursive Listing(-R)
Lists all files and sub-directories in a directory tree.
This traversal of the directory tree is done recursively until there are no subdirectories
left.
ls- xR
cat: Displaying & Creating Files
• Cat(concatenate) command is very frequently used in Linux.
• It reads data from the file and gives its content as output.
• It helps us to create, view, and concatenate files.
1. To view a single file
• Syntax:
cat file_name
• Example: If file_name = [Link]
cat [Link]
(It will display the content of [Link] file)
2. To view multiple files
• Syntax: cat file_name1 file_name2
• Example: If we have two files , file1 and file2.
cat file1 file2
File
This is file2.
3. To view contents of a file preceding with line numbers.
Syntax: cat -n file_name
Example: If our file_name is file2.
cat -n file2
[Link] is file2.
.
4. Create a file and add content
• Syntax: cat > newfile_name
Example: If we want to create a newfile_name = file2.
cat > file2
hello world!!!
Ctrl+D (hold down Ctrl Key and type ‘d‘) to exit
• 5. Copy the contents of one file to another file.
• Syntax: cat [filename-whose-contents-is-to-be-copied] >
[destination-filename]
• (The content will be copied in destination file )
6. Cat command can append the contents of one file to the end of another file.
• Syntax: cat file_name1 >> file_name2
mkdir- make directory
• mkdir’ stands for “make directory,”
• This command can create multiple directories at once as well as set the permissions for the
directories. It is important to note that the user executing this command must have enough
permission to create a directory in the parent directory, or he/she may receive a ‘permission
denied’ error.
• Syntax of `mkdir` Command in Linux
mkdir [options...] [directory_name]
• Here, replace [directory_name] with the desired name of the directory you want to create.
• Example:
mkdir bca
creates a directory named bca
mkdir bca final sims
Creates three sub-directories with one mkdir command
mkdir bca bca/final bca/sims creates the directory tree
Creates three subdirectories –bca and two sub directories under bca. The order of specifying the
arguments is IMPORTANT.A sub-directory cannot be created before creation of its parent directory.
• Sometimes the system refuses to create a directory.
• Example: mkdir test
mkdir: Failed to make directory “test”; Permission denied
This can happen due to these reasons:
1. The directory test may already exist.
2. There may be an ordinary file by that name in the current directory.
3. The permissions set for the current directory don’t permit the creation of files and
directories by the user.
rmdir- Removing Directories
• The ‘rmdir' command in Linux is specifically designed for removing empty directories.
• Unlike the rm command, which can delete both files and directories, rmdir focuses solely on
directories.
• It is a straightforward tool but nonetheless crucial for maintaining a tidy directory structure on
your system.
• It ensures whether the directories are empty or not before deleting it and it will delete only if
those are empty.
• It helps to prevent data loss by accidentally removing the data contained directories.
Syntax:
The basic syntax of the rmdir command is:
rmdir [option]... [directory]...
Here, ‘[directory]…’ refers to one or more directories you wish to remove.
• rmdir bca Directory must be empty.
• rmdir bca/sims bca/final bca
cp-Copying a File
• cp stands for a copy.
• This command is used to copy files or groups of files or directories.
• It creates an exact image of a file on a disk with a different file name.
• cp command requires at least two filenames in its arguments.
Syntax:
cp [OPTION] Source Destination
cp [OPTION] Source Directory
cp [OPTION] Source-1 Source-2 Source-3 Source-n Directory
The first and second syntax is used to copy the Source file to the Destination file or
Directory. The third syntax is used to copy multiple Sources(files) to the Directory.
Two file names
• If the command contains two file names, then it copies the contents of 1st file to the 2nd file. If
the 2nd file doesn’t exist, then first it creates one, and content is copied to it. But if it existed then
it is simply overwritten without any warning. So be careful when you choose the destination file
name.
cp Src_file Dest_file
One or more arguments
• If the command has one or more arguments, specifying file names and following those
arguments, an argument specifying directory name then this command copies each source file
to the destination directory with the same name,
cp Src_file1 Src_file2 Src_file3 Dest_directory
Example:
Suppose there is a directory named bca having a text file [Link], [Link] and a directory name new in
which we are going to copy all files.
•
For the above command to work, Dest directory must exist because cp command
won’t create it.
• Two directory names
• If the command contains two directory names, cp copies all files of the source directory
to the destination directory, creating any files or directories needed. This mode of
operation requires an additional option, typically R, to indicate the recursive copying of
directories.
• cp -R Src_directory Dest_directory
• In the above command, cp behavior depends upon whether Dest_directory exists or not.
If the Dest_directory doesn’t exist, cp creates it and copies content
of Src_directory recursively as it is. But if Dest_directory exists then copy
of Src_directory becomes sub-directory under Dest_directory.
-i(interactive): i stands for Interactive copying. With this option the system first warns the
user before overwriting the destination file. cp prompts for a response, if you press y then it
overwrites the file and with any other option leaves it uncopied.
cp -i [Source_file] [Destination_file]
• cp chap* progs copies all files beginning with chap
rm-Deleting Files
• rm(remove) command deletes one or more files.
• The following commands delete three files:
rm chap01 chap02 chap03
• A file once deleted can’t be recovered .
• rm won’t normally removes a directory, but it can remove a files from one.
• Without using cd command-
rm progs/chap01 progs/chap02
rm * All files gone.
-i (Interactive Deletion): Like in cp , the -i option makes the command ask the user for
confirmation before removing each file, you have to press y for confirm deletion, any other
key leaves the file un-deleted.
rm –i chap01 chap02 chap03
rm: remove chap01 (yes/no)? ?y
rm: remove chap02 (yes/no)? ?n
rm: remove chap03 (yes/no)? ?[Enter]
A y removes the file any other response leaves the file undeleted.
• -r (Recursive Deletion): With -r(or -R) option rm command performs a tree-walk and will
delete all the files and sub-directories recursively of the parent directory. At each stage it
deletes everything it finds. Normally, rm wouldn’t delete the directories but when used with
this option, it will delete.
rm –r *
Deletes all files in the current directory and all its [Link] u don’t have a
backup then these files will be lost forever.
-f (Force Deletion): rm prompts for confirmation removal if a file is write protected.
The -f option overrides this minor protection and removes the file forcefully.
rm –r f*
(deletes everything in the current directory and below)
mv
• As its name suggests this command is used to rename file directories and move files
from one location to another within a file system.
• Two Distinct Functions of `mv` Command
• 1) Renaming a file or directory.
• 2) Moving a file or directory to another locationthe file chap01
mv doesn’t create a copy of the file,it merely renames it .No additional space is
consumed on disk during renaming .
To rename the file:
mv [source_file_name(s)] [Destination_file_name]
To rename the file chap01 to man01,
mv chap01 man01
• mv chap01 chap02 chap03 progs (to move group of files to a directory)
• To rename a directory:
• mv [source_directory_name(s)] [Destination_directory_name]
• EXAMPLE:
mv pis perdir
• -i` (interactive)
The “-i” option makes the “mv” command ask for confirmation before overwriting an
existing file. If the file doesn’t exist, it will simply rename or move it without prompting.
file
• file command is used to determine the type of a file.
file [option] [filename]
Example: Command displays the file type
file [Link]
file [Link]
file [Link]
• option : Command displays the all files’s file type.
file *
The output shows all files in the home directory
file directoryname/*
Vi Editor
The default editor that comes with the UNIX operating system is called vi (visual
editor). Using vi editor, we can edit an existing file or create a new file from scratch.
we can also use this editor to just read a text file. The advanced version of the vi
editor is the vim editor.
How to open VI editor?
To open vi editors, we just need to type the command mentioned below.
vi [file_name]
Here, [file_name] = this is the file name we want to create or to open the pre-existing file.
• Modes of Operation in the vi editor
• There are three modes of operation in vi:
• Command Mode:
• When vi starts up, it is in Command Mode. This mode is where vi interprets any
characters we type as commands and thus does not display them in the window. This
mode allows us to move through a file, and delete, copy, or paste a piece of text. Enter
into Command Mode from any other mode, requires pressing the [Esc] key. If we press
[Esc] when we are already in Command Mode, then vi will beep or flash the screen.
• Insert mode:
• This mode enables you to insert text into the file. Everything that’s typed in this mode is
interpreted as input and finally, it is put in the file. The vi always starts in command
mode. To enter text, you must be in insert mode. To come in insert mode, you simply
type i. To get out of insert mode, press the Esc key, which will put you back into
command mode.
• Last Line Mode (Escape Mode):
• Line Mode is invoked by typing a colon [:], while vi is in Command Mode. The cursor will jump to
the last line of the screen and vi will wait for a command. This mode enables you to perform
tasks such as saving files and executing commands.
Saving and Exiting
:
•Save and Exit
In command mode, type :wq and press Enter.
w stands for write (save) and q stands for quit.
•Save Without Quitting:
In command mode, type :w and press Enter.
•Quit Without Saving:
In command mode, type :q! and press Enter
• NOTE: For more information refer book :Sumitabha Das,
“Unix Concepts and Application”, TMH