Вы находитесь на странице: 1из 14

Linux Commands for Beginners

16/11/2009

Retail / Assurance Services

Pradeep Kumar Sadhaivam


pradeep.sadhasivam@tcs.com
Files & directory creation and access
File Creation
vi - the visual utility is a screen oriented text editor with 2 modes, either command mode
or insert mode.
• vi –r filename creates a file shall be saved when the editor/system crashes.
• vi -R filename Set the read-only editor option to prevent overwriting of the file
• vi –w file_size filename Sets the value of the window size to file_size.
touch - creates a file if it does not exist. Modify the access and modification time of the
already existing file.
• touch filename Change the access and modification time of the file to the current
time. If filename does not exist then it creates a new file with the current date and
time.
• touch –a filename Change only the access time of the existing file to current time.
• touch –m filename Change only the modification time of the existing file
cp – copy one or more files from source to destination. The file is overwritten, if a file with
same name exists.
• cp filename destination Copy the copy file and paste it in the destination location.
• cp filename1 filename2 Copy filename1 and renames the new file to fielname2
• cp –r dir_name destination Copy all the files in dir_name to the destination folder
recursively
mv – move one or more files from source to destination. If a file name exists then it
overwrites the existing file.
• mv filename destination Move filename to the destination path(directory)
• mv filename1 filename2 filename3 destination Move more then one files to the
destination
• mv filename1 filename2 Rename filename1 to filename2
• mv –r dirname1 dirname2 Move all the files in directory to destination directory.

mkdir – create one or more directories if the user has permission in the parent directory.
• mkdir dirname Creates a directory in the current path
• mkdir –m mode dirname Creates a directory with the specified permission mode
File access and permission
chmod - change the access permission of a file for owner, group and others. Only the
owner of a file can change the permission

Permissions Character representation Octal value


read r 4
write w 2
execute x 1

The permission will be set for below user/group


users - u
group - g
others - o

Operators
= (assign permission)
+ (add permission)
- (revoke permission)

• chmod –ugx filename change the permission of the file. u, g, x = rwx


• chmod 755 filename Grants read (4), write(2) and execute(1) permission for user &
read and execute permission for group and others.
• chmod 511 filename Grants reran and execute permission for User & execute
permission alone for group and others.
• chmod u+x filename, chmod g+w filename, chmod u+w, o+r filename Grants the
mentioned permission for the particular user/group.
• chmod u-w filename Revokes write permission for the user of the file.
• chmod o=x filename Assign execute permission alone to others.
chown change the ownership of one or more files to the new owner name.
• chown filename Changes the ownership of the filename
• chgrp –R dirname Changes the ownership of all the files traversing through the files
in the subdirectory
chgrp change the group of one or more files to the newgroup name.
• chgrp filename Changes the group of the filename to new groupname.
• chgrp –R dirname Changes the groupname of all the files traversing through the
files in the subdirectory.

File Manipulation
Viewing File
cat - read one or more files and print them on the standard output
• cat filename Print the mentioned file in the standard output.
• cat –v filename Read the file named filename and prints all the lines including
special characters
• cat –n filename Read the files with all the output lines numbered.
• cat –s filename Read the files but squeeze multiple blank lines to single line.
• cat file1 file2 file3 > Filename Creates filename combining the contents of file1, file2
and file3.
head – displays the starting part of the file. It will display till the specified byte or line
number.
• head filename Prints the first 10 lines of the files to standard output
• head –c byte_count filename Prints the specified byte_count of filename to
standard output
• head –n line_num filename Prints line_num lines of filename to standard output
tail – prints three last 10 lines of the files mentioned. If more than one fie is mentioned
then file header is also included for each file.
• tail filename Displays the last 10 line of the file
• tail –n line_count filename Displays the last line_count from the file.
• tail –c byte_val filename Displays the last byte_val from the last part of the file.
more – displays one page at a time for files with more than a page.
• more filename Scroll through one page at a time though the file.
less – display the content of the file with one page at a time. Allow the user to move one
line/page up or down at a time. Powerful command compared to more command.
• less filename Allows the user to move forward or backward through the file.
pwd – displays the present working directory.
• pwd Print the full pathname of the current working directory
File Comparison and Difference
cmp – check whether 2 given files contents are same/similar. Used to get the char and the
line number that is varied between the files.
• cmp filename1 filename2 Check whether the filename1 and filename 2 are similar
or else will print the byte and line number which is not similar.
• cmp –s filename1 filename2 Check whether the files are similar else return with exit
codes (0- file are identical, 1 – files are different & 2 – files are not accessible) and
will display nothing.
diff – used to find the difference between two files. Can be used to compare files from
directory with files from another directory.
• diff filename1 filename2 compares filename1 and filename2 and prints the output
with < for contents in filename1 and not in filename2 and prints output with > for
contents in filename2 and not in filename1.
• diff dirname1 dirname2 Compares files in each directory and if similar file name
exists exists, then it will compare the contents inside the file

Deleting Files and Directories


rm – delete one or more files or directories
• rm filename Removes the file named filename
• rm file1 file2 file3...... fileN Removes more than one file at a time. It removes all files
from file1 to fileN.
• rm * Removes all the files inside the current directory.
• rm –r dir_name Removes the files inside the directories and directories recursively.
rmdir – used to delete an empty directory.
• rmdir dirname Removes the empty directory

Searching for files and patterns in files


Searching for files
ls - used for checking the files, permissions, modification date, access control, owner,
group of the files or directories
• ls –l Displays long listing of all the files in the directory ls – list a directory content.
Can we used with various options for listing hidden files, all the files inside a
directory, sorted lists etc.
• ls –a Display entry name starting with . (dot) and .. (double dot)
• ls –r Displays the files in reverse order
• ls –lr Displays the long list of the files in reverse order
• ls –ltr Displays the long listing of the file in reverse as per the file modified time.
• ls –la Displays log listing of the files without hiding entries starting with .(dot)
vdir – lists the directory contents. It gives the same output as ls –lb.
find – searches the specified path for files with names matching the specified pattern,
descending through the tree file system.
• find / -name file_name Find the files whose name matches filename
• find / -type file_type Find the files of different type. The character representations
for various files are b (block special file), c (character special file), d (directory), p
(fifo or named pipe), l (symbolic link), s (socket), or f (plain file).
grep – search a line matching a given pattern from one or more files.
• grep pattern filename Searches for the pattern in the given filename
• grep –i pattern filename ignore the case while searching for the pattern
• grep –n pattern filename Prints the line number of the matched pattern in the
original file.
• grep –r pattern dirname Searched for files recursive in the directory
• grep –l pattern dirname Search for files that contains a match of the pattern &
displays it but suppress the other details.
egrep – search for regular expression pattern in one or more files.
• egrep pattern filename searches for a pattern in filename. Filename can be a file or
directory.
• egrep –i pattern filename ignore the case while searching for the pattern
• egrep –n pattern filename Prints the line number of the matched pattern in the
original file.
• egrep –r pattern dirname searched for files recursive in the directory
fgrep – print the lines matching the text string from one or more files. Same as grep – F
but works faster.
• fgrep –i filename ignore the case while searching for the pattern
• fgrep –n pattern filename Prints the line number of the matched pattern in the
original file.
• fgrep –r pattern dirname searched for files recursive in the directory
Formatting the output
cut – split the file into columns separated by a whitespace and prints the specified
column/ columns
• cut –b 10 filename Cuts and prints till tenth byte from each line of filename
• cut –b 5-16 filename Cuts and prints from 5th to 10th byte from each line of
filename
• cut –c 5 filename Cuts and prints till 5th character from each line of filename
• cut –c 5-13 filename Cuts and prints from 5th to 13th byte from each line of
filename
• cut –d field_seperator –f col_to_select Cut the lines into seperate column using the
filed_seperator and displays the col_to_select cloumns alone
join – writes the join of a line from 2 files matching using a common field to the terminal
output.
• join filename1 filename2 Join 2 lines of the sorted files by matching using a
common pattern
• join –i filename1 filename2 Join 2 lines of the sorted files by matching using a
common pattern but ignores case
paste – write sequentially corresponding lines from one or more files to standard output
separated by tab space.
• paste file1 file2 Write the content of file1 and file2 sequentially separated by Tab’s
to standard output.
• paste file1 file2 file3 Write the content of file1 and file2 sequentially separated by
Tab’s to standard output.
uniq – check for the unique lines from the given file and print it to the standard
output/file.
• uniq filename Displays the output of the file removing the duplicate lines.
• uniq –c filename Displays the output of the lines of the file along with the count of
number of occurrence of each lines
• uniq –d filename Displays the duplicate line of the filename
• uniq –i filename Ignores the case while comparing the lines.
tr – translate the character and used to perform character manipulation operations on the
given string.
• tr string1 string2 filename Replace the occurrence of string1 in filename with string2
and print in the standard output.
• tr –d string filename Delete the occurrence of string from filename
• tr –t string1 string2 filename Truncate string1 to the length of string2
echo – display a text or message on the screen.
• echo string displays the string in the output
• echo $val displays the content/number assigned to the variable val
sed – it is a stream editor used to perform text manipulation/transformation on a input
stream and is more efficient which parse only once over the input.
sort - sort the content of the file
• sort filename Sort the content of the filename
• sort –c filename Check whether the content is sorted but do not sort
• sort –g filename Sort as per the numerical value
• sort –b filename Sort the content ignoring the leading blank space
• sort –o op_file filename Sort the content and writes it the op_file instead of
standard output

Compression and Decompression


Compression and Decompression
gzip – Compress the given files. Rename the compressed files to filename.gz
• gzip –l zip_filename Displays the detail such as compressed size, uncompressed
size, compression ratio and uncompressed file name of he compressed file.
• gzip –r filename Compress the files inside the directories by descending into the
directory and compress the files inside it.
• gzip –f zip_filename Force compression or decompression even thought he files
have multiple links or the corresponding file name already exists.
gunzip – Uncompress files compressed using gzip which ends with whose name ends
with .gz, -gz, .z, -z, _z or .Z.
• gunzip zip_filename decompress the compressed file

Viewing compressed files


bzless – used to view the compressed files or plain text files one page at a time.
• bzless filename Displays one page at a time and enables both forward and
backward movement through the file content.
bzmore– used to view the compressed files or plain text files one page at a time.
• bzmore filename Displays one page at a time. Filename can be compressed or
uncompressed file.

Comparison and difference on compressed files


zcat – read one or more files that are compressed using gzip or compress method.
• zcat filename Reads the command line input of the compressed file and displays to
the standard output. It will uncompress more than one files if a directory is
specified.
zcmp - used to invoke the cmp command on the compressed files
• zcmp filename1 filename2 Invokes the cmp command on the compressed file1 and
file2. Prints the byte and line number where the difference occurs.
zdiff – used to invoke diff command on the compressed files
• zcmp filename1 filename2 Invokes the cmp command on the compressed file1 and
file. Printing the bytes and line number the difference occurs.

File System, Disk space usage and System load


df – displays the free disk space available on all the mounted file system.
• df Displays the amount of free disk space.
• df -a Displays the amount of free disk space on all mounted file system.
• df –k Displays the space details in kilobytes.
• df –m Displays the space details in Megabytes.
• df –h Displays the space details in human readable format.
• df . Displays the free disk space in the current file system.

du – check the space usage for all the files in the directory.
• du Prints the space usage of the files
• du filename Prints the space usage of the filename
• du –a Prints the space usage of all the files.
• du –c Prints the space usage of the files and grand total.
• du –h Prints the space usage of the files in human readable format.
• du –k Prints the space usage of the files in kilobytes.
• du –m Prints the space usage of the files in megabytes.
• du –s Prints the total space usage alone.
wc – gives the byte/word/line count of the given file depending upon the options
specified.
• wc –c filename Prints the byte count in filename
• wc -m filename Prints the character count in the filename
• wc –l filename Prints the newline count in the filename
sum – prints the checksum and the blocks for file.
• sum filename Displays the checksum and block counts of the file.
• sum * Displays the checksum and block counts of the file in the current directory.
uptime – prints details about the current time, number of users, system load, how long the
system is running and system load for the past 1, 5 and 15 minute.
top – provide information about the processes using the cpu resource.
• top Displays information about the tasks, memory status and cpu usage
• top –d delay_time Displays the details in a interval of delay_time
• top –n no_of_iteration Displays the details for mentioned number of iteration
alone.
stat – Gives the details of the i-node of the file which is the output of the stat system call.
• stat filename Displays the details about the file such as Inode, size, blocks and
access rights
• stat –f filename Displays the details about the file system instead of the file

Process
bg - send a job mentioned by its job ID to execute in background
• bd <process_id> Sends the process with process_id to execute in background.
fg – brings the command executed in background to foreground.
• fg job_id Brings the process with job_id to foreground and executes it.
usleep – sleep for some specified number of microseconds.
• usleep time_interval The system suspends processing for the specified number of
microseconds. The default value is 1.
sleep – keep the process in waiting state for the specified amount of time before
executing the next command.
• sleep time_interval Sleeps for specified amount of time in seconds
ps – Displays information about the current active process
• ps –ef to display information about all the process on the system.
• ps axjf Display information about the process tree
• ps axms Display information about all the threads.
nice - Sets the priority for a process between -20 (highest priority) and 19 (lowest priority)
with default being 10. without options its displays the current priority.
• nice –n value command<arguments> Executes the commands with lowering the
priority by the value. Administrative user can increase the priority of the process or
commands.
kill –kills the specified processes, sends the specified processes the specified signal (given
as a number or name)
• kill 0 Kill all the process in the current process group are signaled
• kill 1 All the process with process ID larger than 1 are signaled
• kill PID Kill he process with whose pid is equal to PID
• kill -9 PID Used to terminate process that normal kill command fails to signal.

Job scheduling
crontab – schedule a job to execute at a specified time or keep on executing the job after
a specified interval.
• crontab filename Create/Replace your crontab file
• crontab –l username Lists the users crontab file for username
• crontab –e username Edit the users crontab file for username
• crontab –r username Remove the users crontab file for username

File transfer
ftp – its a file transfer protocol. Used to transfer file from and to a remote network.
• ftp remote_hostname Connect to remote machine for transferring files after
successful login.
• ftp –n remote_hostname Enable file transfer which restrain from using auto_login,
check for details in .netrc files for account of remote machine.
sftp – interactive program that securely transfers files. It perform all operations over
encrypted ssh transport.
• sftp dest_hostname Connect to the destination host and opens an interactive
program to carry out the specified operation.
Accessing and working on remote host
rexec – connects the local host to remote host and execute the command at the remote
host.
• rexec remote_host –n command Connects to the remote_host by getting the user
name and password in command line.
• rexec –l username –p password remote_host command Connects to the remote host
after authenticating with the mentioned username and password.
rlogin – connects your terminal on the local host to remote host system.
• rlogin hostname –l user_name Login to remote host with a different user_name.
rsh - connects to the specified remote host and executes the specified command
authorizing the user with the login name and password. Copies the standard input to
remote terminal and displays it to the standard output.
• rsh hostname Login to the remote host.
• rsh –l username Login with username instead of the default name of user executing
the command
ssh – securely log to a remote system and execute the specified command on the
remotely connected system.
• ssh dest_name –p port_number Connects to dest_name host securely using the
specified port number.
telnet – user interface that communicates with remote system using telnet protocol.
• telnet hostname Connects to the remote host.
• telnet –K hostname Connects to remote host but prevents automatic login.
ping - check for the online status of the remote host using echo request. A response
confirms that the host is active and operational.
• ping destination Sends ICMP packet request to destination to check for the online
status. Stops when it receives an interrupt.
• ping –c count destination Stops after sending count number of ICMP request to
destination

Other useful commands


man – displays the online manual pages in a formatted manner.
• man command – displays the details and usage of the command from online
manual pages
• man –M pathlist Specify the list of directories to search for man pages.
• man –a Forces to display all the pages for that command before exiting.
• man –K Search for a specified pattern in all man pages
cal - display a calendar for the current month of the year
• cal <month> <year> Prints the calendar for the month of the year.
• cal –y <year> Prints the calendar of the whole year.
date – display/change the current date and time. The display format of the date and time
can be altered.
• date – Displays the current date and time of the system
who – display the user logged into the system
• who – shows all the user currently logged into the system
whoami – print the current user ID in the standard output.
• whoami – prints the effective userid
finger – display the information about the user along with the information listed in .plan
and .project (hidden) file for the particular user.
• finger –s displays the user information, terminal name and write status.
• finger –l displays all the information displayed for –s option plus details in .plan and
.project file.
passwd – change the password for a mentioned user.
• passwd Ask the user for the current passwd and new password to change. Change
the password if the current password matches the stores password.
time – gives the resource utilization and also gives the time statistics for the command or
script execution.
• time command Gives the time statistics for the execution of the command.
• time script_name Gives the time statistics for the execution of the script_name.
watch – run the given command repeatedly so that we can check the change in output
over time.
clear – clear the content on the terminal screen
env – display the current environment setting or change the setting as specified/execute
the program in the new environment setting.
uname – print information about the operating system/kernel.
• uname Displays the information about the kernel.
• uname –a Displays system information about kernel, node, kernel release, kernel
version, processor type and hardware platform.
• uname –n Displays the network node’s hostname.
• uname –r Displays the kernel release.
• uname –p Displays the Processor type.
• uname –o displays the operating system
su – run a new shell with the mentioned user ID or else it will create a shell for a superuser.
• su group_name Create a shell with the specified group_name
• su –m group_name Preserve the environment variables.
• su –g group_name Specify the primary group for the shell.
sudo - allow a user to execute a command as superuser or a different user if they are a
privileged user
• sudo –l Displays the various commands and authorized user for the each of the
command.
rpm – is a package manager used for installing, upgrading, deleting and querying software
packages.
• rpm –q pkg_name Checks whether the pkg_name is installed on the system
• rpm –i pkg_name Checks whether the pkg_name is installed or else it will install it.
• rpm –u pkg_name Uninstall the previous version and installs the upgraded version.
• rpm –e pkg_name Removes an installed package in the system.

Вам также может понравиться