Unix utilities

From XennisWiki
Jump to: navigation, search

File system

chmod (change permissions of a file)

To change the ownership of a file to everyone use

sudo chmod -R 777 /path/to/file

chown (change owner of a file)

see chown

chgrp (change group of a file)

see chgrp

cp (copy file)

cp path/example.txt here/example_copy.txt

du (directory/file size)

(1) Display directory size in for human readable format (-h) and (2) display file size too

du -h
du -ah

Display the file size in the current directory

du -h --max-depth=1

df (disk space usage)

df -Th

ldd (list dynamic dependencies)

ldd libjavajpeg.so
	linux-vdso.so.1 =>  (0x00007ffdcd35c000)
	libjpeg.so.8 => /usr/lib/x86_64-linux-gnu/libjpeg.so.8 (0x00007fe9d279a000)
	libjava.so => /usr/lib/jvm/./java-8-openjdk-amd64/jre/lib/amd64/libjava.so (0x00007fe9d256b000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fe9d21a1000)
	libjvm.so => not found
	libverify.so => /usr/lib/jvm/./java-8-openjdk-amd64/jre/lib/amd64/libverify.so (0x00007fe9d1f90000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fe9d1d8c000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fe9d2bfe000)
	libjvm.so => not found

ln (make links between files)

Create a symbolic link

ln -s path/to/source/file my/new/link

ls (list files)

Use long listing format (-l) to display information like the owner user and group

ls -l path/example.txt

mkdir (make directory)

Create a directory named example

mkdir example

mv (move file)

mv path/example.txt here/yo.txt

pwd (print working diretory

pwd

rm (remove file)

rm example.txt
rm -r directory

tee

see tee

Processes

at (schedule a command)

Schedule a command (see Cron as well)

echo "./exampe.sh test" | at 1145 jan 12

List all scheduled command by using at or atq

at -l
atq

Show job details

at -c <job-id>

Remove a job from the queue

atrm <job-id>

kill (kill a process)

Kill a specific process by its PID (use ps to display PIDs)

kill <pid>

The command above is the same as kill -TERM 1234. It sends a SIGTERM, i.e. requests the process to terminate. To send SIGKILL use -KILL or its shortcut, i.e. terminate process immediately.

kill -KILL <pid>
kill -9 <pid>

ps (show process status)

(1) Show process status, (2) show all processes (-A) and grep specific entries

ps
ps -A | grep chrome

User environment

uname (operating system name and version)

uname -a

su (switch user)

(1) Switch to user freddy, (2) exit and (3) switch to root user

su - freddy
exit
su -

Text processing

cut (extract sections from each line of input)

Get the first column of a space separated table

cut --delimiter " " --fields 1
# short: cut -d" " -f1

head

Output the first 5 lines of a file

head -n 5 example.txt

paste

Combine pairs of lines (with the separator ,)

paste -s -d ',\n'

sed

see sed

sort

Sort a CSV file with | as seperator by it's third column

sort --key 3 --field-separator='|' <file>

tail

see tail

wc (word count)

Number of lines (-l) of a file

wc -l example.txt

Shell builtins

alias (create shortcut for a command)

(1) Define a alias for a command and (2) use it

alias ch="cd ~/my/long/path/to/my/dir"
cd

To permanently store this shortcut, add this line in the ~/.bashrc

Networking

host (resolve host, DNS lookup)

host example.org

ping

ping example.org

Searching

find (find files)

Find a file
find -name 'example.txt'

Find without error messsages like Permission denied

find -name "jmeter.log" 2>/dev/null

Create a function herefore

find() {
    command find "$@" 2>/dev/null
}
Find a directory
find . -type d -name <dir-name> -print
Find symbolic links
find . -type l -ls

; Find files with extension and delete them
<source lang="bash">
find -name "*.war" -type f -delete

</source>

grep

see grep

Other

last (show last logged in users)

Show the 10 last logged in users

last -10

See also

External links