Bash help command

bash tips doc aide

 0
 0

Displays all environment variables. If you want to get details of a specific variable, use echo $VARIABLE_NAME.

export

Example:

$ export
AWS_HOME=/Users/adnanadnan/.aws
LANG=en_US.UTF-8
LC_CTYPE=en_US.UTF-8
LESS=-R

$ echo $AWS_HOME
/Users/adnanadnan/.aws

whatis shows description for user commands, system calls, library functions, and others in manual pages

whatis something

Example:

$ whatis bash
bash (1)             - GNU Bourne-Again SHell

whereis searches for executables, source files, and manual pages using a database built by system automatically.

whereis name

Example:

$ whereis php
/usr/bin/php

which searches for executables in the directories specified by the environment variable PATH. This command will print the full path of the executable(s).

which program_name 

Example:

$ which php
/c/xampp/php/php

Clears content on window.

It can be used for the following purposes under UNIX or Linux.

  • Display text files on screen
  • Copy text files
  • Combine text files
  • Create new text files
    cat filename
    cat file1 file2 
    cat file1 file2 > newcombinedfile
    cat < file1 > file2 #copy file1 to file2

The chmod command stands for "change mode" and allows you to change the read, write, and execute permissions on your files and folders. For more information on this command check this link.

chmod -options filename

The chown command stands for "change owner", and allows you to change the owner of a given file or folder, which can be a user and a group. Basic usage is simple forward first comes the user (owner), and then the group, delimited by a colon.

chown -options user:group filename

Copies a file from one location to other.

cp filename1 filename2

Where filename1 is the source path to the file and filename2 is the destination path to the file.

Compares files, and lists their differences.

diff filename1 filename2

Determine file type.

file filename

Example:

$ file index.html
 index.html: HTML document, ASCII text

Find files in directory

find directory options pattern

Example:

$ find . -name README.md
$ find /home/user1 -name '*.png'

Un-compresses files compressed by gzip.

gunzip filename

Lets you look at gzipped file without actually having to gunzip it.

gzcat filename

Compresses files.

gzip filename

Outputs the first 10 lines of file

head filename

Check out the printer queue.

lpq

Example:

$ lpq
Rank    Owner   Job     File(s)                         Total Size
active  adnanad 59      demo                            399360 bytes
1st     adnanad 60      (stdin)                         0 bytes

Print the file.

lpr filename

Remove something from the printer queue.

lprm jobnumber

Lists your files. ls has many options: -l lists files in 'long format', which contains the exact size of the file, who owns the file, who has the right to look at it, and when it was last modified. -a lists all files, including hidden files. For more information on this command check this link.

ls option

Example:

$ ls -la
rwxr-xr-x   33 adnan  staff    1122 Mar 27 18:44 .
drwxrwxrwx  60 adnan  staff    2040 Mar 21 15:06 ..
-rw-r--r--@  1 adnan  staff   14340 Mar 23 15:05 .DS_Store
-rw-r--r--   1 adnan  staff     157 Mar 25 18:08 .bumpversion.cfg
-rw-r--r--   1 adnan  staff    6515 Mar 25 18:08 .config.ini
-rw-r--r--   1 adnan  staff    5805 Mar 27 18:44 .config.override.ini
drwxr-xr-x  17 adnan  staff     578 Mar 27 23:36 .git
-rwxr-xr-x   1 adnan  staff    2702 Mar 25 18:08 .gitignore

Shows the first part of a file (move with space and type q to quit).

more filename

Moves a file from one location to other.

mv filename1 filename2

Where filename1 is the source path to the file and filename2 is the destination path to the file.

Also it can be used for rename a file.

mv old_name new_name

Removes a file. Using this command on a directory gives you an error. rm: directory: is a directory To remove a directory you have to pass -r which will remove the content of the directory recursively. Optionally you can use -f flag to force the deletion i.e. without any confirmations etc.

rm filename

Outputs the last 10 lines of file. Use -f to output appended data as the file grows.

tail filename

Updates access and modification time stamps of your file. If it doesn't exists, it'll be created.

touch filename

Example:

$ touch trick.md

awk is the most useful command for handling text files. It operates on an entire file line by line. By default it uses whitespace to separate the fields. The most common syntax for awk command is

awk '/search_pattern/ { action_to_take_if_pattern_matches; }' file_to_parse

Lets take following file /etc/passwd. Here's the sample data that this file contains:

root:x:0:0:root:/root:/usr/bin/zsh
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
bin:x:2:2:bin:/bin:/usr/sbin/nologin
sys:x:3:3:sys:/dev:/usr/sbin/nologin
sync:x:4:65534:sync:/bin:/bin/sync

So now lets get only username from this file. Where -F specifies that on which base we are going to separate the fields. In our case it's :. { print $1 } means print out the first matching field.

awk -F':' '{ print $1 }' /etc/passwd

After running the above command you will get following output.

root
daemon
bin
sys
sync

For more detail on how to use awk, check following link.

Remove sections from each line of files

example.txt

red riding hood went to the park to play

show me columns 2 , 7 , and 9 with a space as a separator

cut -d " " -f2,7,9 example.txt
riding park play

Display a line of text

display "Hello World"

echo Hello World
Hello World

display "Hello World" with newlines between words

echo -ne "Hello\nWorld\n"
Hello
World

Print lines matching a pattern - Extended Expression (alias for: 'grep -E')

example.txt

Lorem ipsum
dolor sit amet, 
consetetur
sadipscing elitr,
sed diam nonumy
eirmod tempor
invidunt ut labore
et dolore magna
aliquyam erat, sed
diam voluptua. At
vero eos et
accusam et justo
duo dolores et ea
rebum. Stet clita
kasd gubergren,
no sea takimata
sanctus est Lorem
ipsum dolor sit
amet.

display lines that have either "Lorem" or "dolor" in them.

egrep '(Lorem|dolor)' example.txt
or
grep -E '(Lorem|dolor)' example.txt
Lorem ipsum
dolor sit amet,
et dolore magna
duo dolores et ea
sanctus est Lorem
ipsum dolor sit

Print lines matching a pattern - FIXED pattern matching (alias for: 'grep -F')

example.txt

Lorem ipsum
dolor sit amet,
consetetur
sadipscing elitr,
sed diam nonumy
eirmod tempor
foo (Lorem|dolor) 
invidunt ut labore
et dolore magna
aliquyam erat, sed
diam voluptua. At
vero eos et
accusam et justo
duo dolores et ea
rebum. Stet clita
kasd gubergren,
no sea takimata
sanctus est Lorem
ipsum dolor sit
amet.

Find the exact string '(Lorem|dolor)' in example.txt

fgrep '(Lorem|dolor)' example.txt
or
grep -F '(Lorem|dolor)' example.txt
foo (Lorem|dolor) 

Simple optimal text formatter

example: example.txt (1 line)

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

output the lines of example.txt to 20 character width

cat example.txt | fmt -w 20
Lorem ipsum
dolor sit amet,
consetetur
sadipscing elitr,
sed diam nonumy
eirmod tempor
invidunt ut labore
et dolore magna
aliquyam erat, sed
diam voluptua. At
vero eos et
accusam et justo
duo dolores et ea
rebum. Stet clita
kasd gubergren,
no sea takimata
sanctus est Lorem
ipsum dolor sit
amet.

Looks for text inside files. You can use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines.

grep pattern filename

Example:

$ grep admin /etc/passwd
_kadmin_admin:*:218:-2:Kerberos Admin Service:/var/empty:/usr/bin/false
_kadmin_changepw:*:219:-2:Kerberos Change Password Service:/var/empty:/usr/bin/false
_krb_kadmin:*:231:-2:Open Directory Kerberos Admin Service:/var/empty:/usr/bin/false

You can also force grep to ignore word case by using -i option. -r can be used to search all files under the specified directory, for example:

$ grep -r admin /etc/

And -w to search for words only. For more detail on grep, check following link.

Number lines of files

example.txt

Lorem ipsum
dolor sit amet,
consetetur
sadipscing elitr,
sed diam nonumy
eirmod tempor
invidunt ut labore
et dolore magna
aliquyam erat, sed
diam voluptua. At
vero eos et
accusam et justo
duo dolores et ea
rebum. Stet clita
kasd gubergren,
no sea takimata
sanctus est Lorem
ipsum dolor sit
amet.

show example.txt with line numbers

nl -s". " example.txt 
     1. Lorem ipsum
     2. dolor sit amet,
     3. consetetur
     4. sadipscing elitr,
     5. sed diam nonumy
     6. eirmod tempor
     7. invidunt ut labore
     8. et dolore magna
     9. aliquyam erat, sed
    10. diam voluptua. At
    11. vero eos et
    12. accusam et justo
    13. duo dolores et ea
    14. rebum. Stet clita
    15. kasd gubergren,
    16. no sea takimata
    17. sanctus est Lorem
    18. ipsum dolor sit
    19. amet.

Stream editor for filtering and transforming text

example.txt

Hello This is a Test 1 2 3 4

replace all spaces with hyphens

sed 's/ /-/g' example.txt
Hello-This-is-a-Test-1-2-3-4

replace all digits with "d"

sed 's/[0-9]/d/g' example.txt
Hello This is a Test d d d d

Sort lines of text files

example.txt

f
b
c
g
a
e
d

sort example.txt

sort example.txt
a
b
c
d
e
f
g

randomize a sorted example.txt

sort example.txt | sort -R
b
f
a
c
d
g
e

Translate or delete characters

example.txt

Hello World Foo Bar Baz!

take all lower case letters and make them upper case

cat example.txt | tr 'a-z' 'A-Z' 
HELLO WORLD FOO BAR BAZ!

take all spaces and make them into newlines

cat example.txt | tr ' ' '\n'
Hello
World
Foo
Bar
Baz!

Report or omit repeated lines

example.txt

a
a
b
a
b
c
d
c

show only unique lines of example.txt (first you need to sort it, otherwise it won't see the overlap)

sort example.txt | uniq
a
b
c
d

show the unique items for each line, and tell me how many instances it found

sort example.txt | uniq -c
    3 a
    2 b
    2 c
    1 d

Tells you how many lines, words and characters there are in a file.

wc filename

Example:

$ wc demo.txt
7459   15915  398400 demo.txt

Where 7459 is lines, 15915 is words and 398400 is characters.

Moves you from one directory to other. Running this

$ cd

moves you to home directory. This command accepts an optional dirname, which moves you to that directory.

cd dirname

Makes a new directory.

mkdir dirname

Tells you which directory you currently are in.

pwd

Lists stopped or background jobs; resume a stopped job in the background.

Shows the month's calendar.

Shows the current date and time.

Shows disk usage.

Gets DNS information for domain.

dig domain

Shows the disk usage of files or directories. For more information on this command check this link

du [option] [filename|directory]

Options:

  • -h (human readable) Displays output it in kilobytes (K), megabytes (M) and gigabytes (G).
  • -s (supress or summarize) Outputs total disk space of a directory and supresses reports for subdirectories.

Example:

du -sh pictures
1.4M pictures

Brings the most recent job in the foreground.

Displays information about user.

finger username

Lists the jobs running in the background, giving the job number.

Lists your last logins of specified user.

last yourUsername

Shows the manual for specified command.

man command

Allows the current logged user to change their password.

Pings host and outputs results.

ping host

Lists your processes.

ps -u yourusername

Use the flags ef. e for every process and f for full listing.

ps -ef

Shows what your disk quota is.

quota -v

Transfer files between a local host and a remote host or between two remote hosts.

copy from local host to remote host

scp source_file user@host:directory/target_file

copy from remote host to local host

scp user@host:directory/source_file target_file
scp -r user@host:directory/source_folder target_folder

This command also accepts an option -P that can be used to connect to specific port.

scp -P port user@host:directory/source_file target_file

ssh (SSH client) is a program for logging into and executing commands on a remote machine.

ssh user@host

This command also accepts an option -p that can be used to connect to specific port.

ssh -p port user@host

Displays your currently active processes.

Shows kernel information.

uname -a

Shows current uptime.

Displays who is online.

Downloads file.

wget file

Return current logged in username.

Gets whois information for domain.

whois domain

Kills (ends) the processes with the ID you gave.

kill PID

Kill all processes with the name.

killall processname

The & symbol instructs the command to run as a background process in a subshell.

command &

nohup stands for "No Hang Up". This allows to run command/process or shell script that can continue running in the background after you log out from a shell.

nohup command

Combine it with & to create background processes

nohup command &

Open bash_profile by running following command nano ~/.bash_profile

alias dockerlogin='ssh www-data@adnan.local -p2222' # add your alias in .bash_profile

nano ~/.bashrc

export hotellogs="/workspace/hotel-api/storage/logs"

source ~/.bashrc
cd $hotellogs

Make your bash scripts more robust by reliably performing cleanup.

function finish {
  # your cleanup here. e.g. kill any forked processes
  jobs -p | xargs kill
}
trap finish EXIT

When you do export FOO = BAR, your variable is only exported in this current shell and all its children, to persist in the future you can simply append in your ~/.bash_profile file the command to export your variable

echo export FOO=BAR >> ~/.bash_profile

You can easily access your scripts by creating a bin folder in your home with mkdir ~/bin, now all the scripts you put in this folder you can access in any directory.

If you can not access, try append the code below in your ~/.bash_profile file and after do source ~/.bash_profile.

    # set PATH so it includes user's private bin if it exists
    if [ -d "$HOME/bin" ] ; then
        PATH="$HOME/bin:$PATH"
    fi

You can easily debug the bash script by passing different options to bash command. For example -n will not run commands and check for syntax errors only. -v echo commands before running them. -x echo commands after command-line processing.

bash -n scriptname
bash -v scriptname
bash -x scriptname

License: CC BY 4.0

Previous Post Next Post