[go: up one dir, main page]

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

DVS Linux Commands

The document provides instructions for 22 common Linux commands and operations: 1. pwd and ls are used to print the present working directory and list files. 2. clear clears the terminal screen. 3. man displays manual pages for commands. 4. cd changes directories while mkdir creates them. 5. rm removes files and rmdir removes empty directories.

Uploaded by

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

DVS Linux Commands

The document provides instructions for 22 common Linux commands and operations: 1. pwd and ls are used to print the present working directory and list files. 2. clear clears the terminal screen. 3. man displays manual pages for commands. 4. cd changes directories while mkdir creates them. 5. rm removes files and rmdir removes empty directories.

Uploaded by

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

1. To know present working directory.

pwd (Print Working Directory)


Ex: /home/training

2. To know files in present working directory

ls (list of files with ascending order)

ls --help (List help page of ls command with their option)

ls -a (hidden files also displayed-those starting with a ".")

ls -F (will add the �/� Character at the end each directory)

ls -l (long list with owner, group, bytes..etc. information)

ls -lh (to know sizes in human readable format)

ls -m (file names with coma separated)

ls -r (list of filenames with Desc order)

ls -R ( Recursive list from all directories)

ls -s (files with block size, asc with filename)

ls -S (sort by file size desc )

ls -lt (-t modification time, desc order)

ls -lrt (latest modification file or directory date as last)

ls -1

ls */ (lists directories and subdirectories)

ls -d */ (lists only directories )

ls | head -3 (lists top 3 files)

ls | tail -5 (lists bottom 5 files)

3. clear
Clear Terminal screen
Ctrl + L (shortcut)

4. man
format and display the on-line manual pages
man ls

5. change directory location


cd /user/training/hadoop (change to hadoop)
cd .. (Change Current directory to parent directory.)
cd - (Move one directory back from where you are now)
cd (Move to users home directory from anywhere)
cd ~ (Move to users home directory from anywhere)
cd / (move to the root directory)
cd ../../ (Move two directory up from where you are now.)
6. Creation of directory/folder (mkdir)
mkdir hadoop (creates a directory hadoop)
mkdir bigdata hadoop (creates a directories bigdata hadoop)
mkdir -v hdfs hive (print a message for each created directory)
mkdir -p hadoop/pig (creates movies(parent) if doesn't exist)
mkdir -p hadoop/{hive, pig}

7. remove/delete file or dir


rm filename (remove file)(case sensitive)
rm -v filename (verbose-explain what is being done)
rm -i filename (prompt before every removal)
rm -f filename (Ignore nonexistant files, and never prompt
before removing)
rm dir (rm: cannot remove `dir1': Is a directory)
rmdir dir (removes empty directories)
rm -r dir_name (removes non-empty directory)
rm -rf dir_name (Ignore nonexistant folders, and never prompt
before removing)
rm -vr dir (verbose-explain what is being done)

8. Copying file or dir

cp origfile newfile (Creates a copy of the file)


If the destination file newfile already exists, it will be overwritten without a
confirmation prompt.
This is the default behavior for all cp operations

cp -i origfile newfile (prompt before overwrite)

cp origfile /directory/subdirectory (The copy of orgfile will be located in the


subdirectory)

cp origfile /directory/subdirectory/newfile

cp *.jpg /directory/subdirectory

cp -r dir1 dir2 (Copy an entire directory structure into another location)

cp -R /one/two /three/four

Copy the directory two (located in the directory /one), and everything two
contains, into the destination directory /three/four. The result will be called
/three/four/two. The directory /three must already exist for the command to
succeed. If the directory four does not already exist in the directory /three, it
will be created.

9 move/rename file

mv file1 file2 (rename file1 to file2)


mv -i file1 file2 (prompt before overwriting)
mv -f file1 file2 (do not prompt before overwriting)
mv file1 ABC (ABC is dir here. so it moves file1 into ABC dir)
mv test1 test2 test3 dir1 ( Move more than on file to directory)

10 touch is used to create, change and modify timestamps of a file


touch file1 (creates an empty (zero byte) new file )
touch file1 file2 file3 (multiple files can be created once)
touch -a file1 (Change File Access and Modification Time. if file is
not there it will create)
touch -c file1 (will not create a file called file1 if it does not
exists)
touch -t 201212101830.55 file2 (creates file2 with a time stamp of
18:30:55 p.m. on December 10, 2012)

11. CAT command (to create/append file or display the content of the file)

cat file1 (Display Contents of File1)


cat file1 file2 (Display Contents of File1 and file2)
cat >file3 (creates a file called file3. Awaits input from user, type desired
text and press CTRL+D )
cat -n file1.txt (displays line number)
cat -e file1.txt ( �$� is shown at the end of everyline)
cat file1.txt > file2.txt (a source text file to another. if file exits it
overwrites ---redirection operator)
cat file1.txt file2.txt > file3.txt (read contents of file1.txt file2.txt
and write the combined text to the file file3.txt)
cat file1.txt >> file2.txt ( append a source text file to another )

12. MORE is a filter for paging through text one screen at a time

more file1 (check for big file only /var/log/Xorg.0.log)


(backward is not possible)
more -d file.txt (prompt-Press space to continue, 'q' to quit.)
more +5 file1.txt (Display the contents of file myfile.txt, beginning
at line 5)
more -5 file1.txt (Specify how many lines to print in the screen for a
given file)

13. Less same as more only but forward and backward both possible

more /var/log/Xorg.0.log (press enter/down arrow and up arrow)


less -5 file.txt

14. head ==> used to display top 10 lines by default

head /var/log/Xorg.0.log (Display the first 10 lines)


head -15 /var/log/Xorg.0.log (Display the first 15 lines )
head -n 15 /var/log/Xorg.0.log (Display the first 15 lines)
head myfile.txt myfile2.txt (Display the first 10 lines of both
myfile.txt and myfile2.txt, with a header before each that indicates the file name)
head -n 5 myfile.txt myfile2.txt (Display the first 5 lines of both
myfile.txt and myfile2.txt)

15. tail ==> used to display bottom 10 lines by default

tail /var/log/Xorg.0.log
tail -15 /var/log/Xorg.0.log (Display the last 15 lines)
tail -f ==> Output the contents of the file as it grows. Useful to monitor
log

16. sed ==> stream editor for filtering and transforming text
sed -n '5,10p' myfile.txt (prints 5 to 10 lines)
sed '10,15d' myfile.txt (exclude line 10 to 15)
sed -n -e '5,7p' -e '10,13p' myfile.txt
sed 's/unix/linux/' myfile.txt (s-substitution)
sed 's/unix/linux/g' myfile.txt (global)
sed 's/unix/linux/gi' myfile.txt (global)
sed '5d' myfile.txt (excludes 5th line)
sed '$d' myfile.txt (excludes last line)
sed '5,8d' myfile.txt (excludes 5th-8th line)
sed '5,$d' myfile.txt (excludes 5th to last line)

17. Grep
grep stands for "global regular expression print
grep 'word' filename
grep 'word' file1 file2 file3

grep -c "word" file.txt (Display the count of number of matches)


grep -l "unix" * (display all file names which has the matched word)
grep -w 'dvs' file1.txt (Checks for the whole words in a file)
grep --color -n "am" test1.txt (line number)

grep --color "am" test1.txt


grep --color "..s" test1.txt (REGEX)
grep --color -i "am" test1.txt (No Case Sensitive)
grep --color -w "..s" test1.txt (only dvs)
We can specify the -i option to perform a case-insensitive match
-egrep
egrep 'apple|banana|orange' *
egrep -i 'apple|banana|orange' *

18. wc - print newline, word, and character counts for a file


wc -l file1.txt (no of lines in a file)
wc -w file1.txt (no of words in a file)
wc -c file1.txt (no of characters in a file)

19. find - search for files in a directory hierarchy


find . -name myFile.txt (search myFile in current folder)
find /home/training -name myFile.txt
find /home -iname myFile.txt (case insensitive search)
find / -type d -name myFile (Find all directories whose name is myFile in /
directory.)
find . -type f -name *.php (Find all PHP Files in Directory)
find . -type f -perm 777 (Find Files With 777 Permissions)
find /tmp -type f -empty (Find all Empty Files)
find /tmp -type d -empty (Find all Empty Directories)
find /tmp -type f -name ".*" (File all Hidden Files)

20. chmod: change Mode (chmod who=permissions filename)


chmod is used to change the permissions of files or directories
ls -l file1.txt (check permission of a file)
chmod u=rwx,g=rx,o=r file1.txt
chmod 764 file1.txt
chmod o=RWX file1.txt (adding permission to other user)
chmod g+x file1.txt (adding execute permission to the group)
chmod u-r file1.txt (removing read access from user)
chmod a=rw file1.txt (read and write by everyone)

21. chown:
changing ownership of a file/directory
ls -l file1.txt (to check ownership)
sudo chown training file1.txt (give permission to training user)

22. shortcuts
Ctrl+C � Cancels the currently running command.
Ctrl+D � log out of current session, similar to exit
Ctrl+W � erases one word in the current line(useful in editors)
Ctrl+U � erases the whole line(useful in editors)

CTRL+F � forward one window


CTRL+B � backward one window
CTRL+D � forward half window
CTRL+U � backward half window

j � navigate forward by one line


k � navigate backward by one line

exit � log out of current session

Ctrl+Z � stops the current command, resume with fg in the foreground or bg in


the background

jobs Lists all jobs (bg & fg)


bg % n Places the current or specified job in the background, where n is
the job ID
fg % n Brings the current or specified job into the foreground, where n
is the job ID
Control-Z Stops the foreground job and places it in the background as a
stopped job

sleep 100 & (To run a job in the background-&)


fg % 1
sleep 100
ctrl+z
bg%1
jobs

23. history -list the last 1000 successful commands with unique id.
history 20 ( list the last 20 commands)
history | less
history | head
history | tail
history -c (to clear the history)
!21 (To run 21st command from history)

24. DATE (display date and time)

date
date -d today
yesterday,tomorrow,sunday,last-sunday,last-week,next-week,last-month,next-
month,last-year,next-year)
date --date="next mon"
date --date="yesterday"
date --date="1 month ago"
date --date="1 year ago"

date +%D (To display Current Date in MM/DD/YY format)


date +%F (To display date in YYYY-MM-DD format)

date +%T (To display time as HH:MM:SS, Note: Hours in 24 Format)


date +"%r" (To display 12-hour clock time)

date +"%m-%d-%y" (display date in dd-mm-yy format)


date +"%d-%m-%Y" (display date in dd-mm-yyyy format)

25. cal (calendar)

cal
cal 2015
cal 2 2015
cal -3 (the previous, current and next month)
cal -y (calendar for whole year)
cal -m (monday as first day)
cal -s (sunday as first day)

26. uptime � current system time,


number of logged in users and
current CPU load
multiple sessions running

27. w� Show who is logged on and what they are doing.


tty1 is desktop login, pts/0 is a pseudo terminal ..
every terminal window is counted as a login

28. who � Report which users are logged in to the system.


whoami � who you are logged in as

29. uname -a � Print information about the current system

30. cat /proc/cpuinfo � cpu information

31. cat /proc/meminfo � memory information

32. df � The �df� command displays the information of device name,


total blocks, total disk space, used disk space, available disk space

df -h (disk usage in human readable format)


df -m (disk usage in MB)

33. du � show directory space usage


du -a both directories and files
du -h � show directory space in human readable format

34. free � Display RAM details in Linux machine


free-k (KB)
free-m (MB)
free-g (GB)
35. Compressing file:
Command : gzip emp.csv
Output : emp.csv.gz
36. Un-compressing file:
COmmand : gunzip emp.csv.gz
Output : emp.csv
37. Compressing multiple files into 1 compressed file:
Command : tar cf file.tar file1 file2 file3 file4 ("tar" stands for tape
archive)
Output : Creates 1 single file named file.tar

tar -tvf demo.tar - list the files

38. Uncompressing tar file :


Command : tar xf file.tar
Output : Extracts all the files from file.tar

39. vi <filename_NEW> or <filename_EXISTING>


If you specify an existing file, then the editor would open it for you to edit.
Else, you can create a new file.

~ shows unused lines


The vi editor opens in command mode
To enter text we should be in insert mode

i Inserts text before the current cursor location(goes into insert mode)
a Write after cursor (goes into insert mode)
A Write at the end of line (goes into insert mode)
o Creates a newline for text entry belowcursor location (goes into insert
mode)
O Creates a new line for text entry above cursor location (goes into insert
mode)
esc Terminate insert mode

:q! exit vi without saving any of the changes


:w save the file but keep it open
:wq save and quit

shift+zz save the file and quit


vi -R filename (open an existing file in the readonly mode)

k moves the cursor up 1 line


J moves the cursor down 1 line
H moves the cursor to the left 1 char position
i moves the cursor to the right 1 char position

40. shell scripting


shell scripting is used to write a series of command for the shell
Shell is a program which interprets user commands through CLI like Terminal

Create a file using a vi editor(or any other editor). Name script file with
extension .sh
Start the script with #! /bin/sh (meaning that the script should always be run
with bash)
Write some code.
Save the script file as filename.sh
For executing the script type bash/sh filename.sh
step1: vi script1.sh
step2: #!/bin/sh
ls
step3: sh scriptsample.sh

The first line tells Unix that the file is to be executed by /bin/sh
The only exception is when the very first line of the file starts with #!

# for comment

x="hello"
echo $x

#!/bin/sh
echo "what is your name?"
read name
echo "How do you do, $name?"
read remark
echo "I am $remark too!"

41. CRON
The basic usage of cron is to execute a job at a specific time

The following is a list of cron directories:


/etc/cron.hourly
/etc/cron.daily
/etc/cron.weekly
/etc/cron.monthly

Copy your shell script �script.sh` or �script� into one of the directories above.
If you need to run the script hourly

crontab -l (To see the running cron jobs)


crontab -u username -l (to see user level cron jobs)

Give the shell script the correct permission. For example, if script is called
�script.sh�, set permission as follows:
cd /etc/cron.daily/
chmod 755 script.sh

1. system wide cron-cron


vi /etc/crontab
2. user level cron
crontab --help
crontab -e (edit user's crontab)
crontab -l (list user's crontab)
crontab -r (delete user's crontab)

crontab -u training -e

* * * * * /usr/bin/uptime >> /home/training/linux/testcron.txt

0 11,16 * * * /usr/bin/uptime >> /home/training/linux/testcron.txt ((11am &


4pm))

0 9-18 * * * /usr/bin/uptime >> /home/training/linux/testcron.txt


(9am to 6pm)

0 9-18 * * 1-5 /usr/bin/uptime >> /home/training/linux/testcron.txt


(9am to 6pm only weekdays)

*/10 * * * * /usr/bin/uptime >> /home/training/linux/testcron.txt


(every 10 min)

1. The number of minutes after the hour (0 to 59)


2. The hour in military time (24 hour) format (0 to 23)
3. The day of the month (1 to 31)
4. The month (1 to 12)
5. The day of the week(0 or 7 is Sun, or use name)
6. The command to run

ps aux | grep cron (to see cron-crond process running)


vi /etc/crontab

su to go to root user

* * * * * root /usr/bin/uptime >> /home/training/linux/testcron.txt

Cronjobs are written in the following format

* * * * * /bin/execute/this/script.sh (run every minute)

The stars represent different date parts in the following order:


minute (from 0 to 59),
hour (from 0 to 23),
day of month (from 1 to 31)
month (from 1 to 12),
day of week (from 0 to 6) (0=Sunday)

* means every

0 1 * * 5 /bin/execute/this/script.sh
(Execute every Friday 1AM)

0 1 * * 1-5 /bin/execute/this/script.sh
(Execute on workdays 1AM)

@reboot Run once, at startup


@yearly Run once a year "0 0 1 1 *"
@annually (same as @yearly)
@monthly Run once a month "0 0 1 * *"
@weekly Run once a week "0 0 * * 0"
@daily Run once a day "0 0 * * *"
@midnight (same as @daily)
@hourly Run once an hour "0 * * * *"

Storing the crontab output

*/10 * * * * /bin/execute/this/script.sh >> /var/log/script_output.log 2>&1


STDOUT is marked 1, STDERR is marked 2. So the following statement tells
Linux to store STDERR in STDOUT as well,
creating one datastream for messages & errors:

mailing the crontab output

MAILTO="yourname@yourdomain.com"

Trashing the crontab output

*/10 * * * * /bin/execute/this/script.sh > /dev/null 2>&1

/dev/null is a special file that discards all data written to it.

You might also like