Various helpful command line commands


Check whether a reboot is required:

ls /var/run/reboot-required

In a bash script, you can use:

if [ -f /var/run/reboot-required ]; then
  echo 'reboot required'

File Management

Copy all files except some in all sub directories

cd ~/movies
find . -type d -exec mkdir ~/copied/{} \;
find . -type f -not -name *.mkv -exec cp {} ~/copied/{} \;

Modify file but preserve modified timestamp

cp -p <file> <file-with-timestamp>
vim <file>
touch -r <file-with-timestamp> <file>

Find all files which were modified within the last month from the current directory

find . -newermt $(date +%Y-%m-%d -d '1 month ago') -type f -print

Find all files modified on the 7th of June, 2006

find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08

Find all files accessed on the 29th of september, 2008

find . -type f -newerat 2008-09-29 ! -newerat 2008-09-30

Find files which had their permission changed on the same day

find . -type f -newerct 2008-09-29 ! -newerct 2008-09-30
rsync -aAXv /origin /dest
rsync -azAX -H --delete --numeric-ids /path/to/source /path/to/dest

Determine size of a folder path

du -hs

Change permissions for all files in a directory tree

find -type f -print0 | xargs -0 chmod 644

Copy all files

rsync -av <source> <dest>

Count all files in a directory (without .files)

ls -1 | wc -l

Change the name of all files like "source_1111_22.jpg" to "dest_1111_22.jpg"

rename 's/source_(\d{4}_\d{2})\.jpg$/dest_$1.jpg/' *

If “source” is “sourca” and “sourcb”, do the following:

rename 's/\w{6}_(\d{4}_\d{2})\.jpg$/dest_$1.jpg/' *

Delete Directory and Files within recursively

find -type d -name <dir> -exec rm -rf {} \;

Refresh display of the last 15 lines of a file every 2 seconds

watch tail -n 15

Linux rename command

Disk Management

# iotop
smartctl -H /dev/sda
blkid /dev/sda1
mount -t cifs -o user=luke // /mnt/linky_share
mdadm --manage /dev/md0 --add /dev/sdb1

Diskspace information:

df -h



usermod -a -G groupName userName

Add physical disk:

VBoxManage internalcommands createrawvmdk -filename /home/bco/.VirtualBox/diskname.vmdk -rawdisk /dev/sda


Find each occurrence of 'foo' (in all lines), and replace it with 'bar'.:


Find each occurrence of 'foo' (in the current line only), and replace it with 'bar':


Search and Replace


To fix the broken arrow keys in insert mode in vi, change the vi default config as follows:

$ vi ~/.exrc
set nocompatible


hostname <newname>
vim /etc/hostname
vim /etc/hosts
vim /usr/share/X11/xorg.conf.d/00-Settings.conf
Option "BlankTime" "600"

Port scan

check for root privileges

Don't forget to change the root password. If any user has UID 0 besides root, they shouldn't. Bad idea. To check:

grep 'x:0:' /etc/passwd

Again, you shouldn't do this but to check if the user is a member of the root group:

grep root /etc/group

To see if anyone can execute commands as root, check sudoers:

cat /etc/sudoers

To check for SUID bit, which allows programs to be executed with root privileges:

find / -perm -04000

Add a service to the init system

Write a script with the LSB tags to be read by the insserv service:

# Provides: myservice
# Required-Start: sshd
# Required-Stop: sshd
# Should-Start:
# Should-Stop:
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start myservice
# Description: Start myservice after sshd is started (example)
myservice -options
exit 0

Name the script myservice, move it to /etc/init.d/, and run the following command as root:

# update.rc-d myservice defaults