Linux Commands
Linux Commands
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
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.
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.
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
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
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
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
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 [email protected] --mail-rcpt [email protected] smtp://mailserver.com