Linux Commands 1.: Alias COMMAND
Linux Commands 1.: Alias COMMAND
Linux Commands 1.: Alias COMMAND
LINUX COMMANDS
1.
alias COMMAND:
alias command allows you to create a shortcut to a command. As the name indicates, you can
set alias/shortcut name for the commands/paths which is too longer to remember.
SYNTAX:
The Syntax is
alias [options] [ AliasName [ =String ] ]
OPTIONS:
-a Removes all alias definitions from the current shell execution environment.
-p Prints the list of aliases in the form alias name=value on standard output.
EXAMPLE:
alias lhost='cd /var/www/html'
alias home='cd /var/www/html/hscripts/linux-commands'
Now if you type home it will take you to the specified folder/directory.
alias c='clear'
2.
cal COMMAND:
cal command is used to display the calendar.
SYNTAX:
The Syntax is
cal [options] [month] [year]
OPTIONS:
Output:
September 2008
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
2. cal -3 5 2008
Output:
Here the cal command displays the calendar of April, May and June month of year 2008.
3.
cat COMMAND:
cat linux command concatenates files and print it on the standard output.
SYNTAX:
The Syntax is
cat [OPTIONS] [FILE]...
OPTIONS:
-A Show all.
-b Omits line numbers for blank space in the output.
-e A $ character will be printed at the end of each line prior to a new line.
-E Displays a $ (dollar sign) at the end of each line.
-n Line numbers for all the output lines.
-s If the output has multiple empty lines it replaces it with one empty line.
-T Displays the tab characters in the output.
Non-printing characters (with the exception of tabs, new-lines and form-
-v
feeds) are printed visibly.
EXAMPLE:
cat > file1.txt
This command creates a new file file1.txt. After typing into the file press control+d (^d)
simultaneously to end the file.
cat >> file1.txt
To append data into the same file use append operator >> to write into the file, else the
file will be overwritten (i.e., all of its contents will be erased).
3. To display a file:
cat file1.txt
cat file1.txt file2.txt
The above cat command will concatenate the two files (file1.txt and file2.txt) and it will
display the output in the screen. Some times the output may not fit the monitor screen. In
such situation you can print those files in a new file or display the file using less
command.
cat file1.txt file2.txt | less
cat file1.txt file2.txt > file3.txt
In the above example the output is redirected to new file file3.txt. The cat command will
create new file file3.txt and store the concatenated output into file3.txt.
4.
cd COMMAND:
cd command is used to change the directory.
SYNTAX:
The Syntax is
cd [directory | ~ | ./ | ../ | - ]
OPTIONS:
1. cd linux-command
This command will take you to the sub-directory(linux-command) from its parent
directory.
2. cd ..
This will change to the parent-directory from the current working directory/sub-directory.
3. cd ~
This command will move to the user's home directory which is "/home/username".
5.
chgrp COMMAND:
chgrp command is used to change the group of the file or directory. This is an admin
command. Root user only can change the group of the file or directory.
SYNTAX:
The Syntax is
chgrp [options] newgroup filename/directoryname
OPTIONS:
Change the permission on files that are in the subdirectories of the directory
-R
that you are currently in.
-c Change the permission for each file.
-f Force. Do not report errors.
EXAMPLE:
1. chgrp hiox test.txt
The group of 'test' directory is root. With -R, the files and its subdirectories also changes
to newgroup hiox.
They above command is used to change the group for the specific file('calc.txt') only.
6.
chmod COMMAND:
chmod command allows you to alter / Change access rights to files and directories. SYNTAX:
The Syntax is
chmod [options] [MODE] FileName
File Permission
# File Permission
0 none
1 execute only
2 write only
ls -alt
This command is used to view your files with what permission they are.
chmod 066 file1.txt
chmod 777 file1.txt
7.
clear COMMAND:
This command clears the terminal screen.
SYNTAX:
The Syntax is
clear
OPTIONS:
There is no options for clearscreen command.
EXAMPLE:
1. clear
alias c='clear'
c is the alias name for clear command.
8.
cp COMMAND:
cp command copy files from one location to another. If the destination is an existing file, then
the file is overwritten; if the destination is an existing directory, the file is copied into the
directory (the directory is not overwritten).
SYNTAX:
The Syntax is
cp [OPTIONS]... SOURCE DEST
cp [OPTIONS]... SOURCE... DIRECTORY
cp [OPTIONS]... --target-directory=DIRECTORY SOURCE...
OPTIONS:
-a same as -dpR.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument.
if an existing destination file cannot be opened, remove it and try
-f
again.
-p same as --preserve=mode,ownership,timestamps.
-- preserve the specified attributes (default:
preserve[=ATTR_LIST mode,ownership,timestamps) and security contexts, if possible
] additional attributes: links, all.
--no-
don't preserve the specified attribute.
preserve=ATTR_LIST
--parents append source path to DIRECTORY.
EXAMPLE:
cp file1 file2
cp -b file1.php file2.php
cp -R scripts scripts1
The above cp command copy the folder and subfolders from scripts to scripts1.
9.
cut COMMAND:
cut command is used to cut out selected fields of each line of a file. The cut command uses
delimiters to determine where to split fields.
SYNTAX:
The Syntax is
cut [options]
OPTIONS:
-c Specifies character positions.
-b Specifies byte positions.
-d flags Specifies the delimiters and fields.
EXAMPLE:
Lets create a file file1.txt and let it have the following data:
Data in file1.txt
This is, an example program,for cut command.
1. cut -c1-3 text.txt
Output:
Thi
Output:
The above command is used to split the fields using delimiter and cut the first two fields.
10.
date COMMAND:
date command prints the date and time.
SYNTAX:
The Syntax is
date [options] [+format] [date]
OPTIONS:
Slowly adjust the time by sss.fff seconds (fff represents fractions of a
-a second). This adjustment can be positive or negative.Only system admin/
super user can adjust the time.
-s date Sets the time and date to the value specfied in the datestring. The datestr may
-string contain the month names, timezones, 'am', 'pm', etc.
-u Display (or set) the date in Greenwich Mean Time (GMT-universal time).
Format:
%a Abbreviated weekday(Tue).
%A Full weekday(Tuesday).
%b Abbreviated month name(Jan).
%B Full month name(January).
%c Country-specific date and time format..
%D Date in the format %m/%d/%y.
%j Julian day of year (001-366).
%n Insert a new line.
%p String to indicate a.m. or p.m.
%T Time in the format %H:%M:%S.
%t Tab space.
%V Week number in year (01-52); start week on Monday.
EXAMPLE:
1. date command
date
The above command will print Wed Jul 23 10:52:34 IST 2008
date -V
The above command will print Wed Oct 08 11:37:23 IST 2008
11.
echo COMMAND:
echo command prints the given input string to standard output.
SYNTAX:
The Syntax is
echo [options..] [string]
OPTIONS:
-n do not output the trailing newline
-e enable interpretation of the backslash-escaped characters listed below
-E disable interpretation of those sequences in STRINGs
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
EXAMPLE:
1. echo command
2. To use backspace:
echo -e "hscripts\tHiox\tIndia"
12.
egrep COMMAND:
egrep command is used to search and find one or more files for lines that match the given
string or word.
SYNTAX:
The Syntax is
egrep [options] pattern [file]
OPTIONS:
-b Print the byte offset of input file before each line of output.
-c Print's the count of line matched.
-e pattern
Searches the pattern list.
list
-h Print matched lines but not filenames.
Ignore changes in case; consider upper- and lower-case letters
-i
equivalent.
-n Print line and line number.
egrep 'hscripts|forums|free' file1.txt
egrep 'free.*images' file1.txt
13.
ifconfig COMMAND:
ifconfig command displays information about the network interfaces attached to the system
and also used to configure the network interface.
SYNTAX:
The Syntax is
ifconfig [options]
OPTIONS:
dispalys information about both active and inactive
-a
Interface
[interface-name] dispalys information about interface
[interface-name] up Activates the interface
[interface-name] down Inactivates the interface
[interface-name] [IP Address] up Assigns IP address to the interface and activates it
EXAMPLE:
ifconfig
eth0
Link encap:Ethernet HWaddr 00:14:85:9C:CC:55
inet addr:192.168.0.12 Bcast:192.168.0.255 Mask:255.255.255.0
inet6 addr: fe80::214:85ff:fe9c:cc55/64 Scope:Link
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:7856 errors:0 dropped:0 overruns:0 frame:0
TX packets:7856 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:492016 (480.4 KiB) TX bytes:398 (398.0 b)
Interrupt:201 Memory:e1000000-0
lo
Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0
inet6 addr: ::1/128 Scope:Host
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:1455 errors:0 dropped:0 overruns:0 frame:0
TX packets:5 errors:0 dropped:0 overruns:0 carrier:0
RX bytes:1917382 (1.8 MiB) TX bytes:1917382 (1.8 MiB)
In above output:
Link encap:Ethernet -Specifies the type Interface
HWaddr 00:14:85:9C:CC:55 -Specifies the Hardware or MAC address
inet addr:192.168.0.12 -Specifies the IP address assigend to network-interface
The above command will Assign IP address 192.168.0.12 to Ethernet card with name
eth0.
14.
kill COMMAND:
kill command is used to kill the background process.
SYNTAX:
The Syntax is
kill [-s] [-l] %pid
OPTIONS:
Specify the signal to send. The signal may be given as a signal name or
-s
number.
Write all values of signal supported by the implementation, if no operand is
-l
given.
-pid Process id or job id.
-9 Force to kill a process.
EXAMPLE:
Step by Step process:
15.
mkdir COMMAND:
mkdir command is used to create one or more directories.
SYNTAX:
The Syntax is
mkdir [options] directories
OPTIONS:
-m Set the access mode for the new directories.
-p Create intervening parent directories if they don't exist.
-v Print help message for each directory created.
EXAMPLE:
1. Create directory:
mkdir test
The above command is used to create the directory 'test' and set the read and write
permission.
16.
man COMMAND:
man command which is short for manual, provides in depth information about the requested
command (or) allows users to search for commands related to a particular keyword.
SYNTAX:
The Syntax is
man commandname [options]
OPTIONS:
-a Print a one-line help message and exit.
-k Searches for keywords in all of the manuals available..
EXAMPLE:
1. man mkdir
17.
mv COMMAND:
mv command which is short for move. It is used to move/rename file from one directory to
another. mv command is different from cp command as it completely removes the file from the
source and moves to the directory specified, where cp command just copies the content from one
file to another.
SYNTAX:
The Syntax is
mv [-f] [-i] oldname newname
OPTIONS:
This will not prompt before overwriting (equivalent to --reply=yes). mv -f will
-f
move the file(s) without prompting even if it is writing over an existing target.
-i Prompts before overwriting another file.
EXAMPLE:
mv file1.txt file2.txt
2. To move a directory
mv hscripts tmp
In the above line mv command moves all the files, directories and sub-directories from
hscripts folder/directory to tmp directory if the tmp directory already exists. If there is no
tmp directory it rename's the hscripts directory as tmp directory.
mv file1.txt tmp/file2.txt newdir
This command moves the files file1.txt from the current directory and file2.txt from the
tmp folder/directory to newdir.
18.
pwd COMMAND:
pwd - Print Working Directory. pwd command prints the full filename of the current working
directory.
SYNTAX:
The Syntax is
pwd [options]
OPTIONS:
-P The pathname printed will not contain symbolic links.
-L The pathname printed may contain symbolic links.
EXAMPLE:
pwd
If you are working in home directory then, pwd command displays the current working
directory as /home.
19.
printf COMMAND:
printf command is used to write formatted output.
SYNTAX:
The Syntax is
printf format [argument]....
OPTIONS:
The format characters and their meanings are:
\b Backspace.
\n Newline.
\t Horizontal tab
\v Vertical tab.
EXAMPLE:
1. printf "hello\n"
2. printf "hel\blo"
Output:
helo
20.
ps COMMAND:
ps command is used to report the process status. ps is the short name for Process Status.
SYNTAX:
The Syntax is
ps [options]
OPTIONS:
List information about all processes most frequently requested: all those
-a
except process group leaders and processes not associated with a terminal..
-A or e List information for all processes.
-d List information about all processes except session leaders.
-e List information about every process now running.
-f Generates a full listing.
-j Print session ID and process group ID.
-l Generate a long listing.
EXAMPLE:
1. ps
Output:
In the above example, typing ps alone would list the current running processes.
2. ps -f
Output:
21.
rmdir COMMAND:
rmdir command is used to delete/remove a directory and its subdirectories.
SYNTAX:
The Syntax is
rmdir [options..] Directory
OPTIONS:
Allow users to remove the directory dirname and its parent directories which
-p
become empty.
EXAMPLE:
1. To delete/remove a directory
rmdir tmp
rmdir command will remove/delete the directory tmp if the directory is empty.
rm -ir tmp
This command recursively removes the contents of all subdirectories of the tmp
directory, prompting you regarding the removal of each file, and then removes the tmp
directory itself.
22.
rm COMMAND:
rm linux command is used to remove/delete the file from the directory.
SYNTAX:
The Syntax is
rm [options..] [file | directory]
OPTIONS:
-f Remove all files in a directory without prompting the user.
-i Interactive. With this option, rm prompts for confirmation before removing
any files.
Recursively remove directories and subdirectories in the argument list. The
directory will be emptied of files and removed. The user is normally
-r (or) -R
prompted for removal of any write-protected files which the directory
contains.
EXAMPLE:
rm file1.txt
rm -ir tmp
This rm command recursively removes the contents of all subdirectories of the tmp
directory, prompting you regarding the removal of each file, and then removes the tmp
directory itself.
rm file1.txt file2.txt
23.
whois COMMAND:
whois command lists the information about the domain owner of the given domain.
SYNTAX:
The Syntax is
whois [option] query
OPTIONS:
-h Host which holds the identification information in its database.
-p connect to the specified port.
EXAMPLE:
1. whois hscripts.com
Output:
The above command will produce the following output.
[Querying whois.internic.net]
[Redirected to whois.PublicDomainRegistry.com]
[Querying whois.PublicDomainRegistry.com]
[whois.PublicDomainRegistry.com]
Registration Service Provided By: HIOX INDIA
Contact: +91.4226547769
Registrant:
HIOX INDIA
Rajesh Kumar ([email protected])
32, North Street, Krishnapuram, Singanallur
Coimbatore
tamil nadu,641005
IN
Tel. +91.04225547769
Administrative Contact:
HIOX INDIA
Rajesh Kumar ([email protected])
32, North Street, Krishnapuram, Singanallur
Coimbatore
tamil nadu,641005
IN
Tel. +91.04225547769
Technical Contact:
HIOX INDIA
Rajesh Kumar ([email protected])
32, North Street, Krishnapuram, Singanallur
Coimbatore
tamil nadu,641005
IN
Tel. +91.04225547769
Billing Contact:
HIOX INDIA
Rajesh Kumar ([email protected])
32, North Street, Krishnapuram, Singanallur
Coimbatore
tamil nadu,641005
IN
Tel. +91.04225547769
Status:ACTIVE
This sample output was produced at 11 a.m. The "." indicates activity within the last
minute.
24.
who COMMAND:
who command can list the names of users currently logged in, their terminal, the time they
have been logged in, and the name of the host from which they have logged in.
SYNTAX:
The Syntax is
who [options] [file]
OPTIONS:
Print the username of the invoking user, The 'am' and 'i' must be space
am i
separated.
-b Prints time of last system boot.
-d print dead processes.
-H Print column headings above the output.
Include idle time as HOURS:MINUTES. An idle time of . indicates activity
-i
within the last minute.
-m Same as who am i.
-q Prints only the usernames and the user count/total no of users logged in.
-T,-w Include user's message status in the output.
EXAMPLE:
1. who -uH
Output:
This sample output was produced at 11 a.m. The "." indiacates activity within the last
minute.
2. who am i