====== Various helpful command line commands ======
===== System =====
Check whether a reboot is required:
ls /var/run/reboot-required
In a bash script, you can use:
#!/bin/bash
if [ -f /var/run/reboot-required ]; then
echo 'reboot required'
fi
===== Copy files between servers =====
- Copy between servers with user privileges$ rsync -avz -e 'ssh -p 22' dir user@x.x.x.x:/path/to/dir/
- Copy between servers with root privileges$ rsync -avz -e 'ssh -p 22 -i /home/user/.ssh/id_ecdsa' --rsync-path="sudo rsync" dir user@x.x.x.x:/path/to/dir/
* also enable the user on the target machine to run rsync with root privileges, to do so create file ''/etc/sudoers.d/rsync'' (line 1) with content (line 2)$ sudo visudo /etc/sudoers.d/rsync
user ALL = NOPASSWD:/usr/bin/rsync
- Copy from cloud$ rsync -avz -e 'ssh -p 22 -i /path/to/identity/file' --info=progress2 user@x.x.x.x:/path/to/dir/ /local/dir/
===== 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
vim
touch -r
==== Find all files containing specific text ====
* Since you have GNU find/xargs, this is a safe way for xargs to read filenames:find . -type f -print0 | xargs -0 grep "some string"
* If you only want the filenames that have a matching line without showing the matching line:find . -type f -print0 | xargs -0 grep -l "some string"
==== 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
==== Copy all files excluding hardlinks ====
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