[go: up one dir, main page]

0% found this document useful (0 votes)
189 views10 pages

Linux Commands

Uploaded by

rajdutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
189 views10 pages

Linux Commands

Uploaded by

rajdutt
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

 cd : Change Directory

 cd / : Go to root directory
 cd - : Move one directory back from where you are now
 cd .. : Change current directory to parent directory
 cd -- : Show last working directory from where we moved
 cd ../ ../ : Move two directories up from where you are now
 cd ~ : Move to users home directory from anywhere
 cd . : Change working directory to current working directory

 ls : List files & directories


 ls –t : Open last file edited
 ls -1 : Display one file per line
 ls -d */ : Display only directories
 ls -R : Displays directoris and subdirectories under them
 ls -p | grep -v / : Display only files
 ls –l : Display all information about files/Directories
 File type : - normal file; d directory; s socket file; l link file
 File permissions
 Number of links
 Owner
 Group
 Size
 Last modified date & time
 File name
 ls –lh : Display file size in human readable format i.e. M, MB or KB
 ls –ld : Display directory information
 ls –lt : Order files based on last modified time
 ls –ltr : Order files based on last modified time in reverse order
 ls –lS : Sort files by size
 ls –a : Display hidden files
 ls –R : Display files recursively
 ls –i : Display file Inode number
 ls –n : Display files using UID & GUID
 ls –F : Visual classification of files with colors
 Blue : Output directories
 Green : Soft link
 alias ll = ls –l : You can take some required option and make it as aliases.

 mkdir : make directory


 mkdir test : Create ‘test’ directory
 mkdir test1 test2 : Creates multiple directories
 mkdir –m a=rwx mydir : Create ‘mydir’ directory and set its permission that all users may read, write &
execute.
 Mkdir –p /home/test/a/b: Create directory ‘home/test/a/b’. If parent directory does not exist create one.

 rmdir : Remove directory


 rmdir mydir : Removes the directory ‘mydir’

 rm : Remove files and directories


 rm file1 : Remove file1
 rm file2 file2 : Remove multiple files i.e. file1, file2..
 rm –i file1 : Delete the file interactively. The command will prompt before deleting a file.
 rm –d dir : Deletes an empty directory
 rm –r dir : Deletes a directory recursively using ‘-r’ option
 rm –ri dir : Deletes the files and sub-directories interactively
 rm –f file1 : Deletes file forcefully irrespective of its permissions. –f will not work for write protected
directories.
 rm –I file1 : Prompt once before deleting more than 3 files or recursive delete.
 rm –I files : prompt before every file removal
 rm with REGULAR EXPRESSION
 rm -- \ -store : delete a file with hyphen (-) symbol

 cp : Copy file
 cp project/source.txt project1/destination.txt : copy file from source to destination
 cp project/source.txt project1/ : copy file with same name from source to destination
 cp –r project project1/ : Recursively copy all contents from project (dir) to project1
 cp –I test.txt /home/office/ : Create hard links to files instead of copying them
[Please note: Hardlinks cannot be created on into directories. i.e. files will have same inode number but not
the directories]
 cp –s /home/office/documents/test.txt test.txt : Create symbolic links to files.
[Please note: Creating symlinks can only be done in current directory.]
 cp –n readme.txt /home/office/bin/ : Don’t overwrite an existing file
 cp –i readme.txt /home/office/bin/ : Confirm before overwriting
 cp –p sample.txt test/ : Preserve attributes of file or directory while copying.
It is also possible to preserve only the required properties like mode, ownership, timestamps, etc.
cp –preserve=mode sample.txt test/
 cp –vu *.txt /home/office : Copy only when the source file is newer
 cp –backup=simple –v *.txt ../office : Create backup of each copied file
[--backup = none,off; numbere,t; existing,nil; simple,never]
 cp –attributes-only readme.txt –v ../office : Copy only file attributes

 mv : Move files
 mv test.txt /home/office : Move file
 mv test.txt test1.txt test2.txt /home/office : Move multiple files
 mv dir_1/ /home/office/ : Moving directory
 mv test1.txt test2.txt : Renames test1 file to test2
 mv dir_1 dir_2 : Renames dir_1 to dir_2
 mv –v *.txt /home/office : Print the file movement in detail
 mv –i file_1.txt /home/office : Will provide notification whether to overwrite an existing
file (interactive mode)
 mv –uv *.txt /home/office : Will perform update only if source is newer than
destination file.
 mv –vn *.txt /home/office : Do not overwrite an existing file
 mv –bv *.txt /home/office : Create backup when copying
 -f : will move file forcefully.

 pwd : print working directory


 pwd : print current working directory
 pwd –L : Display logical current working directory
 pwd –P : Display physical current working directory
 echo “$PWD $OLDPWD” : Display current and previous working directory

 less : Used to search through files and text.

 more : It displays output in terminal one page at a time.

 head : Outputs the first part of file


 head readme.txt : Displays first 10 lines of file
 head -15 readme.txt : Displays first 15 lines of file
 head readme1.txt readme2.txt : Displays first 10 lines of both the files
 head –n 5 readme1.txt readme2.txt : Displays only first 5 lines of both the files
 head –c 20 readme.txt : Outputs only first 20 bytes (characters) of file
 head –n 5k readme.txt : Displays first 5 thousand lines of file
 head –n 4 *.txt : Displays first 4 lines of every file in directory

 tail : Outputs last part of file


 tail readme.txt –n 100 : Outputs last 100 lines of file
 tail –f readme.txt : Outputs last 10 lines of file and monitors file for updates.
tail then continues to output any new lines that are added
to readme.txt
 head –n 20 readme.txt | tail –n 5 : Print 15th line to 20th line from readme file
 tail –f –s 5 readme.txt : determine the sleep interval for –f

 cat : Create single or multiple files, view contains, concatenate &


redirect.
 cat readme.txt : Display contents of file
 cat file1.txt file2.txt : View contents of multiple files
 cat > file1.txt : Create a file with cat command
 cat –n file.txt : Display line numbers in file
 cat file1.txt; file2.txt : Display multiple files at once
 cat file1 > file2 : Redirect standard output of a file to a new file
 cat file1 >> file2 : Append standard output at the end of file2
 cat file1 file2 file3 > file4 : redirecting multiple file contains into single file
 cat –s file1 : Display contents of file, omitting any repeated blank lines.

 touch : Used to create, change, modify timestamps of a file


 touch file.txt : Creates an empty file with 0 byte.
 touch file1 file2 file3 : Create multiple files
 touch –a readme.txt : Changes file access and modification time
 touch –m readme : Will update the last modification time of file
 touch –c –t YYDDHHMM readme : explicitly set the access & modification time
 touch –r readme redit : Will update the timestamp of redit with the timestamp of
readme.
 touch –t YYMMDDHH.SS readme : Create a file using specified time

 wc : Find out word count, newline count


 wc readme.txt : Prints number of lines, words & bytes of the file.
 wc –l readme : Prints number of lines in the file.
 wc –w readme : Prints number of words in the file
 wc –c readme : Prints number of characters in the file
 wc –L readme : Display length of longest line

 nl : Numbers the line in a file


 nl readme : Prints line# for each row
 nl –i2 readme : Increments line numbers with value using –i option
 nl –s. readme : Add string after line numbers
 nl –w1 readme : Use a different column for line numbers
 nl –bpA readme : Number lines beginning with A
STYLE :
 a : Number all lines
 t : Number only non-empty lines
 n : Number no lines
 pRE: Number only lines that contain Regular
Expression (RE).
 nl –nln readme.txt : FORMAT for inserting line number
 ln : left justified, no leading zeros
 rn : right justified, no leading zeros

 sudo : It allows a user with proper permissions to execute a command as another user, such as supuser.
 sudo mount /dev/had /mnt : Executing command as super user.
 sudo –k : ‘Kill’ sudo authentication for the current user.

 chmod : Change permissions of files or directories.


 chmod options permissions filename
 Roles: u=user, g=group, o=others, a=all
 Permissions: r=read (4), w=write(2), x=execute (1)
 + : Add specified permission to the mentioned user/group/others/all
 - : Remove specified permission to the mentioned user/group/others/all
 = : Replicate specified permission to the mentioned user/group/others/all
 chmod u=rwx, g=rx, o=r readme .txt : user can read, write, execute, group can read & execute
and others can read only.
 chmod u+w filename : Add write permissions to a group
 chmod u=g filename : Replicate user permissions to a group
 chmod u-x filename : Removing execute permissions to a user
 chmod –R 755 directory-name : Apply permissions to all files under directory recursively.

 Chown : change owner of a file


 chown option user: group file/folder
 chown himanshu file_1 : Changed owner of the file_1 to Himanshu
 chown :family file_1 : Changed group to family
 chown himanshu:friends file_1 : Changed both owner and group of the file
 chown –h root:friends tmp_link : Forcefully change the owner/group of symbolic file
 chown --from=himanshu root file_1 : Change owner to root only if existing owner is himanshu
 chown --from=:friends :family file_1 : Change group to family only if existing group is friends
 chown --reference=file tmpfile : Copy the owner/group settings from one file to another
 chown –R himanshu:friends file_1 : Change owner/group of files by traveling the directories recursively
 chown –v –R guest:friends file_1 : List all the changes made by chown command

 chgrp : Change group ownership of file/files.


 chgrp options groupname file/foldername
 chgrp hrgroup file_1 : Changed groupname to ‘hrgroup’
 chgrp –hR staff /office/files : Change the ownership of /office/files, and all subdirectories, to
group staff.
 chgrp –f apache /var/apache : Change group ownership forcefully

 chroot : Runs a command or an interactive shell from another directory and treats that dir. As root.
 chdir : System function for changing the current working directory

 chage : To change user’s password aging and expiry information.


 chage [-m mindays] [-M maxdays] [-d lastday] [-I inactive] [-E expiredate] [-W warndays] user

 Passwd : To change the password of a user account.


 Files
 /etc/passwd : User account info.
 /etc/shadow : Secure user account info.
 /etc/pam.d/passwd : PAM configuration for passwd
 passwd : Change your own password
 passwd Raj : Change password for Raj user
 password –S Raj : Display password status information
 PS = Password Set, LK = Password Locked, NP = No Password)
 password –Sa : Display password info. for all accounts.
 passwd –d Raj : Remove password of Raj
 passwd –e Raj : set password expiry immediately
 passwd –l Raj : Lock the password of system user
 passwd –u Raj : unlock the user password
 passwd –i 10 Raj : Setting inactive days using –i option
 passwd –n 90 Raj : Set minimum days to change password
 passwd –w 12 Raj : Set warning days before expiring password

 finger : Check the information of any user.


 finger –s user : Display’s user login name, real name, terminal name, write status, idle time,
login time, office location, phone number.
 finger –l user : Displays –s info. as well as user’s home directory, home phone number,
login shell, mail status, and the contents of file from user home directory.
 finger –p user : same as –I except it does not include contents of file from user HD.
 finger –m user : avoid returning info. of users with almost similar names.

 login : Begins a session on the system


 File
 /var/run/tmp :
 /var/log/wtmp :
 /etc/passwd :
 /etc/shadow :
 /etc/motd :
 /etc/nologin :
 /etc/ttype :
 newgrp :

 useradd : It’s a low-level utility that is used for creating/adding user accounts in linux
 useradd command does following after you run it –
 edits /etc/passwd, /etc/shadow, /etc/group & /etc/gshadow files for the newly created user account.
 Creates & populates a home directory for the new user.
 Sets permissions & ownership to home directory
 useradd test_user : Adds a new user called test_user
 useradd –d /office/documents test_user : Create a user with different home directory
 useradd –u 999 navin : Create a user with specific user ID. In linux, every user has
its own UID.
 useradd –u 1000 –g 500 raj : Create a user with specific group ID
 useradd –G admin, webadmin, developer Raj : Adding user Raj to multiple groups
 useradd –M Raj : Add a user without home directory
 useradd –e 2017-12-21 Raj : Create a user with account expiry date
 useradd –e 2017-12-21 –f 45 Raj : Create a user with password expiry date
 useradd –c “Raj Dutt” Raj : Add user with custom content e.g. phone#, full name,etc.
 useradd –m –d /var/www/ravi –s /bin/bash –U ravi : Add user with specific home directory, default shell
and group with same name as user.
 useradd –m –d /var/www/ravi –s /usr/bin/nologin ravi : Add user with home directory, but no shell
 useradd –M –N –r –s /bin/false raj : -M creates user w/o home directory, -N create username
without group. –r creates system user.
 /etc/passwd : User account information
 /etc/shadow : Secure user account information
 /etc/group : Group account information
 /etc/gshadow : Secure group account information
 /etc/default/useradd : Default values for account creation
 /etc/skel : Directory containing default files
 /etc/login.defs : Shadow password suite configuration
 Exit Status
 0 : Everything was completed successfully.
 1 : Couldn’t update the password file
 2 : Syntax of command was invalid
 3 : One or more options were given invalid argument
 4 : user ID is already in use
 6 : Specified group doesn’t exits
 9 : Username already in use
 10 : Couldn’t update the group file
 12 : Couldn’t create the home directory
 14 : Couldn’t update SE linux user mapping

 usermod : It is used to modify or change any attributes of an already created user account.
 usermod –c “This is comment” Raj : Adding information to user account.
 usermod –d /var/www/ Raj : Change user home directory to /var/www
 usermod –e 2017-12-21 Raj : Set user account expiry date
 usermod –g Cybage Raj : Change user group to Cybage
 usermod –a –G Cogni Raj : User is added to supplementary Cogni group along with
Cybage group
[NOTE: Never use only –G option only. As it will assign you a
new group deleting existing/old groups for that user.]
 usermod –l Rajdutt Raj : User login name is changed from Raj to Rajdutt
 usermod –L Raj : Lock system user account. After the account is locked we
can’t login using the password.
 usermond –U Raj : unlock user account
 usermod –d /var/raj –m Raj : Move user home directory to new location
 usermod –p password Raj : Create un-encrypted <password>
 usermod –s /bin/bash Raj : Change user shell to /bin/bash
 usermod –u 888 Raj : Change UserID to 888
 usermod –g 777 Raj : Change GID of user

 userdel : It is a low-level utility for removing users.


 Files :
 /etc/group
 /etc/login.defs
 /etc/passwd
 /etc/shadow
 Exit Status
 0 : Success
 1 : Couln’t update passwd file
 2 : Invalid command syntax
 6 : Specified user doesn’t exist
 8 : Couldn’t delete user because specified user is currently logged in
 10 : Couldn’t update group file
 12 : Couldn’t remove home directory
 userdel Raj : Remove user/account name Raj
 userdel –r Raj : Files in user’s home directory will be removed along with
the home directory itself and the user’s mail spool.
 -Z : Remove any SELinux user mapping for the user’s login.
 deluser : Removes users from the system.
 Return Value
 0 : The action was successfully executed.
 1 : The user to delete was not a system account. No action was performed.
 2 : There is no such user. No action was performed.
 3 : There is no such group. No action was performed.
 4 : Internal error. No action was performed.
 5 : The group to delete is not empty. No action was performed.
 6 : The user does not belong to the specified group. No action was performed.
 7 : You cannot remove a user from its primary group. No action was performed.
 8 : The required perl-package 'perl modules' is not installed. This package is required to
perform the requested actions. No action was performed.
 9 : For removing the root account the parameter "--force" is required. No action was
performed.
 /etc/deluser.conf
 deluser Raj : delete the user account Raj
 deluser --remove-home Raj : delete the user account and remove’s user home
directory.
 deluser –force Raj : deletes user account Raj even if that user is
logged-in.
 deluser –backup-to /directory Raj : Deletes the user account Raj and backs up their file
in a tar archive called Raj.tar which is created in the
directory ‘/directory’

 groupadd : Creates a new group account using the values specified in command line.
 -f : (force) Simply exit with success status if the specified group already exists.
 -g : (GID) Numeric value of group’s ID
 -K : Overrides /etc/login.defs defaults
 -o : It permits to add a group with a non-unique GID
 -p : Encrypted password. Option not recommended as password will be visible.
 -r : Create a System group.
 -R : Apply changes in CHROOT_DIR directory.
 groupdel : Used by a superuser or root to remove a group
 groupdel group

 groupmod : Enables a superuser or root to modify a group


 groupmod –g 600 Cybage : Change group ID
 groupmod –n Cognizant Cybage : Change groupname to Cognizant
 groupmod –o 0 Cybage : Change the group ID with non-unique
 groupmod –p adada@234328479 Cybage: Change the group password

 gpasswd : To administer /etc/group & /etc/gshadow


 gpasswd –a Raj Cybage : Add raj to Cybage group
 gpasswd –M Raj Suraj Nitesh Cybage : Add multiple users to Cybage group
 gpasswd –d Suraj Cybage : delete Suraj from Cybage
 gpasswd –A Raj Cybage : Set Raj as group administrator
 gpasswd –R Cybage : Restrict group to just the specified members
 gpasswd –r Cybage : Remove a group password
 gpasswd Cybage : Changing password of Cybage group

 rlogin
 rsh
 stty
 crontab : cron is a daemon to run schedule tasks.
 Crontab format :
- MIN(0-59) HOUR(0-23) DOM(1-31) MON(1-12) DOW(0-6) CMD(Any command to be execute)
 SPECIAL CHARACTERS :
- * : Every day/Month/day of the week
- - : To define range
- / : 1st Field/10 meaning every ten minute or increment of range
- , : To separate items
 SPECIAL STRINGS :
- @reboot : Command will run when the system reboot
- @daily : once per day or may use @midnight
- @weekly : once per week
- @yearly : Once per year. We can use @annually keyword also.
 Crontab Files :
- /etc/cron.allow
- /etc/cron.deny
 && : Multiple commands with double amper-sand ‘&&’
- @ daily <command1> && <command2>
 crontab -e : edit your crontab
 crontab –l : list the contents of your crontab
 crontab –r : Remove your crontab
 sudo crontab -u charles -e : Edit the crontab of user named Charles

 at : Execute commands at specified time.


 at 10.00 AM : Schedule task at coming 10am
 at 10.00 AM Sun : Schedule task at 10am coming Sunday
 at 10.00 AM July 25 : Schedule task at 10.00 AM on coming 25th July
 at 10.00 Am next month: Schedule task at 10am on same date next month
 at 10.00am tomorrow : Schedule task at 10.00am tomorrow
 at now + 1 hour : Schedule task to execute just after 1 hour
 at now + 1 week : Schedule task to execute just after 1 week
 at midnight : Schedule task to execute at mid night
 atrm 3 : Remove any at job using atrm using their job id
 wget:
It retrieves file from world wide web using protocols like Http, Https & Ftp.
It works on slow or unstable network connections.
It automatically starts download where it was left off in case of network problem and will keep trying until file
has retrieves completely.
 It downloads file recursively.
 Single file download
 wget https://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz
 Download file with different name: Send messages to log file
 wget -o wget.zip https://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz\
 Download multiple files with http & ftp
 Wget https://ftp.gnu.org/gnu/wget/wget-1.5.3.tar.gz ftp://ftp.gnu.org/gnu/wget/wget-
1.5.3.tar.gz
 Read URL’s from a file
 wget -i /wget/temp.txt
 Resume uncompleted download
 wget -c http://mirrors.hns.net.in/centos/6.3/isos/x86/centos-6.3-x86_64-LiveDVD.iso
 Download files in background
 wget -b /wget/log.txt ftp://ftp.iinet.net.au/debian.ssxx..xxx..iso
 Restrict download speed limits and all logs will be created under /wget/log.txt
 wget -c –limit-rate=100k /wget/log.txt ftp://ftp.sadjsd......iso
 Restricted ftp and http downloads with username and password
 wget –http-user=raj -http-password=test@123 http://xyz.iso
 Do not download multiple verions. E.g. if we try to download same file twice it won’t with option
nc = No Clobber
 wget -nc https://sdahdh.iso
 Don’t check SSL/TLS certificate
 wget https://localhost/file.txt --no-check
 Change progress bar
 wget –progress=dot https://
 Retry failed download: -t option followed by number of retries
 wget -t 10 https://xyz..iso
 Wait time: Delay the next retry to download
 time wget -w 10 -i to-download.txt
 Hide output
 wget -q https://scsckl.iso
 If download takes too much of time, we can abort and cancel it automatically
 wget -T 30 https://xyz.iso
 Download a file but save it in specific folder
 wget –directory-prefix=folder/subfolder xyz.com
 Download a file but only if the version on server is newer than local copy
 wget –continue—timestamping text.zip
 Download a list of sequentially numbered files from a server
 wget http://example.com/images/{1..20}.jpg
 Download all MP3 files from a sub directory
 wget –level=1 --recursive --noparent --accept mp3, MP3 http://example.com
 Download all images from a website in a common folder
 wget –directory-prefix=files/pictures –no-directories --recursive --no-clobber --accept
jpg,gif,png,jpeg http://example.com/images
 Download pdf files from a website through recursion but stay within specific domains
 wget --mirror –domains=abc.com, files.com accept=pdf http://abc.com/
 Download all files from a website but exclude a few directories
 wget --recursive --no-clobber --no-parent --exclude-directories /forums, /support
http://example.com

 Find the size of a file without downloading it


 wget --spider --server-response http://example.com/file.iso
 Download a file and display the content on screen without saving it locally
 wget --output-document --quiet http://example.com/file.iso

 wget Vs curl
 curl supports much more protocols and platforms.
 curl supports more authentication methods
 wget has major ability to download recursively, can recover from prematurely broken transfer and continue
downloading.

 curl : It is a tool to transfer data from or to a server using http/s, ftp/s, scp, sftp, ldap, telnet.
 Download a single file
 curl http://www.centos.org
 Save the cURL output to a file
 curl -o mygettext.html http://ww.gnu.org /software/gettext/manual/gettext.html
 Fetch multiple files at a time
 curl -o url1 -o url2
 Follow HTTP location headers with -L options
 curl -L http://www.google.com
 Continue/Resume Previous download
 curl -o http://ww.xyz.html
 curl -c http://www.xyz.html
 Limit the rate of data transfer
 curl --limit-rate 1000B -O http://www.xyz.html
 Download a file only if its modified before/after the given time
 curl -z 21-dec-11 http://xyz.com
 Pass HTTP authentication in Curl
 curl -u username:password URL
 Download files from Ftp server
 curl -u ftpuser:ftpasswd -0 ftp://ftp_server/public_html
 List/Download using Ranges
 curl ftp://ftp.abc.com/pool/main/[a-z]
 Upload files to Ftp server
 curl -u ftpuser:ftpasswd -T “{file1,file2}” ftp://ftp.testserver.com
 Use proxy to download a file
 curl -x proxyserver.test.com:3128 http://google.co.in
 Send mail using SMTP protocol
 curl -mail-from test@abc.com --mail-rcpt test@xyz.com smtp://mailserver.com

You might also like