HomeHowTo › Some Useful Unix Commands

Some Useful Unix Commands

Here’s a list of some useful unix commands with brief explanations of the parameters. “/foo/bar/” will be used as a placeholder for a directory name and “file” for a particular file name. This list will be extended from time to time.

 

Determining the Size of a File or Folder

1
2
3
4
5
6
7
8
9
# show folder size
du -hs /foo/bar/

# show file size
du -hs /foo/bar/file

# INFO
# -h = human-readable output
# -s = show size of the whole folder instead of listing every subfolder

 

Finding Files or Folders

1
2
3
4
5
6
7
8
9
10
11
12
13
# find any file or folder matching the given pattern
find /foo/bar/ -iname "*file*" -print 2> /dev/null

# only find files
find /foo/bar/ -iname "*file*" -type f -print 2> /dev/null

# only find folders
find /foo/bar/ -iname "*file*" -type d -print 2> /dev/null

# INFO
# -iname = specify filename or pattern to look for, not case-sensitive (alternative: -name)
# -print = print the path of the files/folders that have been found (alternative: -ls)
# 2> /dev/null = do not print error messages (i.e. permission denied)

 

Finding Files Containing a Certain String

1
2
3
4
5
6
7
# show all files that contain the specified string
grep -rl "string" /foo/bar

# INFO
# -r = search the folder recursively
# -l = look for a certain string
# -i = case insensitive

 

Creating and Extracting TGZ (TAR.GZ) Archives

1
2
3
4
5
6
7
8
9
10
11
12
13
# create a tgz archive of a folder
tar -cvzf file.tgz /foor/bar/

# extract a tgz archive to a specific folder
tar -xvzf file.tgz -C /foo/bar/

# INFO
# -c = create a new archive
# -x = extract an archive
# -v = list all the processed files
# -z = use gzip (un)compression
# -f = specify the name of the archive
# -C = specify the directory to extract to (will be the current directory if omitted)

 

Creating and Extracting ZIP Archives

1
2
3
4
5
6
7
8
9
# create a zip archive
zip -r5 file.zip /foo/bar/

# extract a zip archive to the current folder
unzip file.zip

# INFO
# -r = include all subfolders
# -{0-9} = set compression rate (0=store, 1=lowest/fastest, 9=highest/slowest)

 

Mounting Windows Shares

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# cifs utilities have to be installed first
sudo apt-get install cifs-utils

# on older systems smbfs and/or smbclient might be needed instead
sudo apt-get install smbfs smbclient

# mounting can be done as follows
mount -t cifs //172.16.0.1/share /mnt

# mounting with authentication
mount -t cifs //172.16.0.1/share /mnt -o username="user name",password="password"

# mounting with authentication, without typing the password in cleartext
mount -t cifs //172.16.0.1/share /mnt -o user="user name"

# unmount the share
umount -l /mnt

# INFO
# -l = force unmount if the device is still busy (lazy unmount)

 

Mounting Windows Shares (Permanently)

1
2
3
4
5
6
7
8
9
10
11
12
13
# cifs utilities have to be installed first
sudo apt-get install cifs-utils

# add this to /etc/fstab
//servername/share    /mnt    cifs    username=user,password=pass,iocharset=utf8,sec=ntlm,auto,nodev,ro    0    0

# test if it works
mount -a

# INFO
# if one of the paths contains a space character, use \040 instead of whitespace or %20
# instead of username= and password= you can use credentials= to specify a file that contains the data
# nodev (don't interprete device files) and ro (readonly) are optional

 

Package Management

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# clean the package information cache
apt-get clean

# update the package information
apt-get update

# look for a package
apt-cache search apache2
apt-cache search ^apache2$
apt-cache search "http server"

# get more information about a package
apt-cache show apache2

# install a package
apt-get install apache2

# remove a package
apt-get remove apache2

# remove a package and its configuration files
apt-get remove apache2 --purge

 

Determining the Gateway Address

1
2
3
4
5
netstat -nr | grep default

# INFO
# -n = show numerical addresses
# -r = display the kernel routing table

 

Creating User Accounts

1
2
3
4
5
6
7
8
9
10
# create user foo
useradd -m -s /bin/bash foo

# set a password for the new user
passwd foo

# INFO
# -m = create home directory
# -s = set the shell to be used
# -D = use default settings

 

Adding Users to Groups

1
2
3
4
5
# create group bar
groupadd bar

# add user foo to group bar
adduser foo bar

 

Creating Dummy Files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 100kb file
dd bs=1k count=100 if=/dev/urandom of=dummy.txt

# 100mb file
dd bs=1m count=100 if=/dev/urandom of=dummy.txt

# INFO
# bs = blocksize in bytes
# count = number of blocks
# if = input file
# of = output file

# 100mb file (alternative method)
head -c 104857600 /dev/urandom > dummy.txt

# INFO
# -c = number of bytes

 

CPU Stress Test

1
2
3
4
5
# generate cpu usage (create multiple instances for extra usage)
yes > /dev/null &

# stop
killall yes

Leave a Comment