List of useful linux command

Create user with home and bash

sudo useradd -m -d /home/username -s /bin/bash -p password username
sudo passwd username

List and files manipulations

Grep continious stream

To grep on a tail -f :

tail -f file | grep --line-buffered my_pattern

Find and list all files with date prior to a week before

find . -mount -type f -mtime +7 -exec ls -la {} \;

Then remove with the same :

find . -mount -type f -mtime +7 -exec rm {} \;

Counting them :

find . -mount -type f -mtime +7 | wc -l

Oldest file

find -type f -printf '%T+ %p\n' | sort | head -n 1

Source

Services

List all service and their bootstart status

systemctl list-unit-files --type=service

or

ls /lib/systemd/system/*.service /etc/systemd/system/*.service

Source

Vi remove first 5 characters in each line

:%s/^.\{0,5\}// should do the trick. It also handles cases where there are less than 5 characters.

You need to use -h and to not have a slash at the end

chown -h root:root linkfolder

Rsync compare folders

rsync -avun --delete $TARGET $SOURCE  |grep "^deleting "

Will give you a list of files that do not exist in the target-directory.

Source

Read file content from inside an archive

tar zxfO archive.tgz file1.csv

You can even pipe it to wc -l command for example to count le number of lines

tar zxfO archive.tgz file1.csv | wc -l
tree -ifpugDs ./ | grep '^\[l'

Most recent file in a directory

ls -Art | tail -n 1

https://stackoverflow.com/questions/1015678/linux-most-recent-file-in-a-directory

Deal with file name starting with hyphen -

Two ways:

mv -- -weirdfilename.zip normalfilename.zip

mv ./-weirdfilename.zip normalfilename.zip

Source

Tar ignore mounted FS

Tar option :

--one-file-system
              stay in local file system when creating archive

List program with file open that can hog space

/usr/sbin/lsof | grep deleted

ou

lsof +L1

Why is space not being freed from disk after deleting a file]

Find a file by hash

Check if file has same hash and same size

wanted_size=$(stat -c %s d1/x1)
wanted_hash=$(sha256sum <d1/x1)
find d2 -type f -size "${wanted_size}c" -execdir sh -c 'test "$(sha256sum <"$0")" = "$1"' {} "$wanted_hash" \; -print

Source

Get the weather in your terminal

Create an alias with the following command and change the city

alias weather='curl -4 http://wttr.in/Paris'

Add colors to man

Add this in .bashrc/.zshrc to add colours to man

export LESS_TERMCAP_mb=$'\e[1;32m'
export LESS_TERMCAP_md=$'\e[1;32m'
export LESS_TERMCAP_me=$'\e[0m'
export LESS_TERMCAP_se=$'\e[0m'
export LESS_TERMCAP_so=$'\e[01;33m'
export LESS_TERMCAP_ue=$'\e[0m'
export LESS_TERMCAP_us=$'\e[1;4;31m'

Mount

Umount device is busy

umount -l /dev/sdb1

man umount
-l Lazy unmount. Detach the filesystem from the filesystem hierarchy now, and cleanup all references to the filesystem as soon as it is not busy anymore.
(Requires kernel 2.4.11 or later.)

From the comment :

To see what process lock the device :

lsof /dev/sdb1

Source

Convert png to webp

find . -name "*.png" | parallel -eta cwebp  -q 80 {} -o {.}.webp && rm {} -f

Reverse

find . -name "*.webp" | parallel -eta cwebp  -q 80 {} -o {.}.png && rm {} -f

Source

Find open ports

sudo netstat -nlp
fuser -n tcp 4005

Rename files with folder name as prefix

#!/bin/bash

for i in */;do
    cd "$i" || return;
    for j in *.png;do
        newfilename="${i::-1}_${j}";
        echo "New filename : $newfilename";
        rename -v "s/$j/$newfilename/" "$j";
    done;
    cd ../;
done

Strip filename after first underscore

for f in "${files[@]}"; do echo "${f/_*.jpg/.jpg}"; done

Source

Diff remote and local folders

diff -y <(ssh remote-server find /remote/folder/ -printf '"%8s %P\n"') \
     <(find local/folder/ -printf '%8s %P\n')