Linux Commands
1) ssh - First open the ubuntu terminal / For windows use putty and then open terminal
cd Downloads/ - where the key is kept that directory.
Use nano for creating file / opening file.
Example- nano singlenode.pem and hit enter
Now copy the pem file data and paste it in nano singlenode.pem file for windows, for linux
just exit(Ctrl X).
Now in AWS instance click on Connect and copy chmod 400 singlenode.pem Hit enter
Now copy this
ssh -i "singlenode.pem" [email protected]
ssh is a protocol
-i is an identifier
singlenode.pem is a key
ubuntu is uername
ec2-3-87-30-245.compute-1.amazonaws.com is a DNS server
2) pwd : It prints current working directory
● pwd -P It prints the parent working directory
3) whoami : It displays username of current user
4) mkdir <directory> : It creates an empty directory
● mkdir -p <directory> : It make parent directories also if it does not exist
5) cd
● cd .. : It is used to change to the parent directory
● cd - : It changes to previous directory
● cd <directory name> : It changes to other directory where you want to go
● cd ~ : It changes to home directory
● cd : It will change to home directory
6) ls : It lists the content
● ls -l : It lists the content in long listing format
● ls -al : It lists all sub content in long listing format
● ls -ltr - It sorts the data chronologically from older to newer (bottom)
● ls -R : It lists the content recursively
● ls -F : It lists the content and classify them
7) man <cmd> - It shows the manual of command. Eg: man cat
8) alias : It display all aliases for current user
● alias <statement> : It will make alias Eg: alias c='clear'
● unalias <alias> : It will remove alias. Eg: unalias c
9) touch test.txt - It will create a zero-byte file, if it doesn't exist
● touch -t <mm-dd-yyyy> <filename> - It changes timestamp of a file
10) cat - It concatenates the files
● cat <filename> - It dumps the contents of 'key.txt' to STDOUT
● cat <filename1> <filename2> : It dumps both files to STDOUT
● cat file1 file2 > file3 - It creates a new catenated file
● cat file1 file2 >> abc : It appends a file
● cat <filename> | more : It displays the contents of the file with forward paging
● less key.txt : To scroll forward: Ctrl-f
To scroll backward: Ctrl-b
To end of file: G
To quit less command: q
11) mv - It is used to moves files and renames files
● mv file.txt file1.txt - It renames a file
12) rm - It removes files/directories
● rm <filename> : It will remove the file.
● rm -rf <filename> : It removes recursively and enforces the file.
● rm -r directory : It removes directory
13) stat - It reveals statistics of files
● stat <filename> : It reveals full attributes of the file
14) find - It is used to find files using search patterns
● find /home/cloudage -name 'filename / directory name'
Note: 'find' can search for fields returned by the 'stat' command
● find /home/cloudage -name *cache* : It finds the file with word “cache”
● find /home/cloudage/hadoop -size +1k : It find files larger than 1k in
/home/cloudage/hadoop
● find /home/cloudage/ -name '*.txt' : It will find all files with extension .txt
find file in a last month directory
find . -maxdepth 1 -newermt "2016-12-06"
Find all Empty Files
To find all empty files under certain path
find /tmp -type f -empty
Find all Empty Directories
find /tmp -type d -empty
Find Last 50 Days Modified Files
find / -mtime 50
Find Last 50 Days Accessed Files
find / -atime 50
Find Changed Files in Last 1 Hour
find / -cmin -60
Find Modified Files in Last 1 Hour
find / -mmin -60
Find 50MB Files
find / -size 50M
Find Size between 50MB – 100MB
find / -size +50M -size -100M
15) seq - It echoes a sequence of numbers
● seq 1000 > 1thousand.txt - It creates a file with numbers 1-1000
16) su - It is used to switch users
● su - It is used to switch to another user.
17) head : It displays opening lines of text files.
18) tail - It displays the closing lines of text files
19) wc - It counts words and optionally lines of text files
● wc -l <filename> : It prints the no of lines in a file.
20) file - It determines the file type
● file /home/cloudage key1.txt
21) tty - It reveals the current terminal
22) cp - It copies files
● cp <filename> <directoryname>/ : It copies a file to a directory, by default 'cp'
does NOT preserve the original modification time
● cp file.txt file1.txt : It is used to copy files.
● cp -a : It is used to copy directories.
● cp -v <filename> <directory-name> : It paste the copied file in directory.
23) history : It display the list of the last 274 commands
● !262: It runs the 262 command
24) aspell -c <filename>: It check the spelling in the file
25) whereis : It display all paths
a. whereis <directory/filename> : It displays where the file/directory is located
26) hostname : It displays hostname name of the user
27) id : It display id info of current user
● id -u : It display user id of current user
● id -un : It display username of current user
● id -g : It display group id of current user
● id -gn : It display groupname of current user
28) uptime : It display for how long the system has been running
29) users : It display no. of users currently logged in
30) who : It display users logged in the system with their
respective terminals and time since logged in
31) w : It displays details which files are open on which
terminal
32) zip : It is used to compress the file
● zip filename.zip *.txt : It compresses the file
● zip -p password filename.zip *.txt : It password protect the file
zip -e filename.zip *.txt
33) unzip : It is used to unzip the zip file
● unzip filename.zip *.txt : It unzips the file
● unzip --ql key.zip *.txt : It quietly unzips the file
34) gzip : It compresses the file
● gzip -c filename.txt > filename.txt.gz
35) tar : It creates a tar file
● tar -cf filename.tar *.txt : It will create a tar file.
● tar -xf filename.tar *.txt : It is used to extract the compressed file
tar -cvfz um.tar.gz um.img
-c – to create archive, -v – verbose, -f for archiving file, -z to compress
files in gzip format.
● tar -czf filename.tar.gz directory name : It create a tar with gzip compression
36) ps : It displays your currently actively processes
● ps aux | grep 'telnet' : It will find all processes id related to telnet process
37) top : It displays all running processes
38) ip addr show: It displays all network interfaces and ip address
39) netstat -tupl : It lists all active listening ports
40) wget file link : It is used to download file
41) locate filename: It find all instances of file.
42) scp filename directory name : It securely copies the file
43) du
● du -ah : It displays disk usage in human readable format
● du -sh : It displays total disk usage in current directory
44) echo $PATH : It displays the $PATH environment variable
a. Use top command with ‘u‘ option will display specific User process details.
Top -u afshin
b. Highlight Running Process in Top
Press ‘z‘ option in running top command will display running process in color
which may help you to identified running process easily.
c. Sort by CPU Utilisation
Press (Shift+P) to sort processes as per CPU utilization
d. Save Top Command Results
To save the running top command results output to a file /root/.toprc
top -n 1 -b > top-output.txt
cat top-output.txt
Where n is number of iterations
Linux chmod example
PERMISSION EXAMPLE
U G W
rwx rwx rwx chmod 777 filename
rwx rwx r-x chmod 775 filename
rwx r-x r-x chmod 755 filename
rw- rw- r-- chmod 664 filename
rw- r-- r-- chmod 644 filename
Ls -l filename
# NOTE: Use 777 sparingly!
LEGEND
U = User
G = Group
W = World
r = Read
w = write
x = execute
- = no access
45) du
a) Specify Maximum Depth
We can make this more useful by specifying how deep we should check with
--max-depth, which can be shortened to the -d option. By setting this to 1, we will
look 1 directory deep and display the size in use of all directories within the
current location.
Du -d 1
b) Human Readable Output
du -h -d 1 /
c) Show Disk Usage For Files
Now we can see the space used within the directories as well as the
specific files within.
du -h -d 2 -a
d) Report Total Disk Space
With the -c option we can have the total space reported at the bottom of the
output
du -h -d 2 -c
e) Sort Output By Disk Usage Size
While not built into the du command, we can pipe it to the sort command in
order to list files in order of file size, such as smallest to largest. This is
extremely useful as we can search the whole disk and order the output
based on file size, allowing us to quickly locate large files.
du -h -a / | sort -h | tail -n 15
f) Show Last Modification Time
While searching for files using disk space to potentially be removed it can
be beneficial to view the last modified time stamp. For example if used disk
space is increasing over time then the most recently modified files are a
good place to investigate, for instance we may have a large log file that is
constantly being modified and written to.
du -h -d 1 --time -a
g) Only Show Larger Than Specified Size
This may be helpful if we want to search for files larger than a specified
size which would be useful in finding large single files that may be using
the majority of your disk space.
du -h -t 100M -a /
h) Exclude Pattern From Search
With the --exclude option we can optionally not show anything that
matches a specified pattern in the search results
du -h -d 1 -a --exclude="*.txt"
i) Piping du
This will search your entire filesystem sort the results by size and then show
only the top ten results. It’s essentially a shortcut for the top ten largest files on
your machine
du -a / | sort -n -r | head -n 10
46) df
a) Running df
If we run the df command with no options, it will print out disk usage information
for all mounted file systems
Df
b) Print Totals
So far with df we can print out the usage of each individual file system. With the
--total option we can get an overall result
df --total -h