Useful Linux Commands

WGET

Downloads a single file/folder or an entire site in a single cmd

# download entire html including images
wget -E -H -k -p https://www.google.com

# continue download from the middle
wget -c http:///.zip

# download in background
wget -b http:///.zip

# download entire website and its contents including landing pages
wget --mirror -p --convert-links -P ./folder http://www.google.com

cURL

Download multiple files through URL/links

# -o is for output and google.html is the output file & format
curl -o google.html https://www.google.com

# -# shows the progress
curl -# -o google.com https://www.google.com

# -k ignores the ssl certificate shows the progress
curl -k -o google.com https://www.google.com

CHMOD

Individual 7 (rwx/4+2+1) is a combination of permissions 4, 2, and 1; where, 4 – read, 2 – write, and 1 – execute

# check if the file(s) have read/write access
ls -la
ls -l 

# give read, write, and execute access to user, group, and others
chmod 777 
chmod a=rwx 

# give read, write, and execute access to all the files under a folder recursively
chmod -R 777 

# check if the files have read/write access
# USER
chmod u=rw 
chmod 500 
# GROUP
chmod g=rw 
chmod 050 
# OTHER
chmod o=rw 
chmod 005 

UID

UID helps you switch between users

[LINUX]
# list users with uid
cat /etc/passwd

[MAC]
# lists users 
dscl . -list /Users

# list users with uid
dscl . -list /Users UniqueID 

# get specific user id
dscl . -read /Users/prashanthrajjapa UniqueID

GREP

Global regular expression parser/print

# list all the matching files with text
ls * | grep 

# print the lines containing given text
grep --color  

# print the lines containing given text with line number
grep --color -n  

# print the lines containing with text from all the matching files
grep --color  *

# ignore case-sensitive
grep --color -i  

# search in all the sub directories recursively
grep --color -i -r  

# match any character in the middle using .*
grep --color  *

WC

To know the number of word counts, lines, and characters in a file

# array 1 represents total number of lines
# array 2 represents total number of words
# array 3 represents total number of characters
wc 
wc < 

# LINES
wc  -l
# WORDS
wc  -w
# CHARS
wc  -c

PS

To view the status of the running processes

# lists all the running process with PID number
ps -A
ps -A | less

# BSD syntax to list all process with full details (owner, %MEM, start time, PID, etc.,)
ps axu
ps aux
ps axu | grep firefox

# check specific process run
ps -A | grep firefox

# check process start time
ps -ef | grep firefox
ps -ef | grep firefox | less

KILL

To kill the running process

# kill a process using process ID
kill 

# kill a process using name
pkill firefox

# force kill a process using name
killall -9 
(e.g., killall -9 chromedriver)

SYMLINK

Set pseudo path for the existing one

# create symlink
ln -s  
(e.g., ln -s /Applications/Firefox.app/ firefox)

# create/update symlink
ln -sf  

TTY

Print current terminal source

# create symlink
tty

TOP

Troubleshoot CPU memory usage

# check instant cpu memory usage live
top -F -R -o cpu

# check instant cpu memory usage live
top -o rsize

HTOP

Advanced version of top; it has to installed externally and not comes by default

# check instant cpu memory usage live
sudo apt-get install htop
htop

AWK

Helps you write logic in the cmd line and play with text

# print whole line 
ls -la | awk '{print $0}'

# print first field
ls -la | awk '{print $1}'
ls -l . | grep "^-" | awk '{print $1}'

UNIQ

Simplify your work by getting line count, duplicates and uniqueness from a file

# prints all the unique lines in a file
uniq -u your_text_file.format

# prints all the duplicate lines in a file
uniq -u your_text_file.format

# prints all the lines with duplicate counts in a file 
uniq -c your_text_file.format

COMPRESS

Zip and unzip files

# compress a file 
compress file_name.format

# compress a file and print percentage reduction in size
compress -v file_name.format

# uncompress a file
uncompress file_name.format.Z

# uncompress a file and print percentage reduction in size
uncompress -v file_name.format.Z

PRINT FILE SIZE 

Print file size

# readable format to see the size of file
ls -lh file_name.format

# alternate way to see the file size
du -sh file_name.format

FIND

Locate directory(ies) and it’s files in the local machine

find ~/ directory_name 

DB Connect

Command line DB conenction

# mysql db connect
mysql -u username -ppassword -h hostname
mysql> show databases;
mysql> use dbname;
mysql> show tables;

# mysql db connect with port and dbname
mysql -u username -ppassword -h hostname -P port -D dbname

PS1 – Primary Prompt

Run set of cmds on launching terminal/cmd prompt. PS1 is the primary cmd prompt, PS2 is the secondary cmd prompt, and PS3 is the least used cmd prompt.

# ps1 to change the terminal color 
LIGHT GREEN
export PS1="\e[0;32m\e[6m\u@\h \w> \e[m "
BRIGHT GREEN
export PS1="\e[0;32m\e[1m\u@\h \w> \e[m "
UNDERLINE GREEN
export PS1="\e[0;36m\e[4m\u@\h \w> \e[m "
BACKGROUND GREEN
export PS1="\e[7;32m\e[1m\u@\h \w> \e[m "
BLINK GREEN
export PS1="\e[5;32m\e[1m\u@\h \w> \e[m "
VALUE IN GREEN
export PS1="\e[0;32m\e[1m\u@\h \w> \e[5m "
STANDARD GREEN
export PS1="\e[1;32m\e[1m\u@\h \w> \e[0m "
more details..
  • To make the above cmds effective, try copy & paste inside bash_profile
# set as environment variable
sudo vi ~/.bash_profile
export PS1="\e[1;32m\e[1m\u@\h \w> \e[0m "
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s