This is a list of linux commands which are not commonly used but
still very good to know.
This is neither a complete list nor a cheat sheet of any kind. This is a
reference for myself and something to show to others if they ask “Why
are you using only the commandline?” again.
Searching through a folder recursively and print every occurance with file name and line number:
grep -rin "SearchText" foldername
r = recursive, i = case insensitive, n = display line numbers
history | grep "sudo"
find / -type f -iname "passwd"
mat2 --inplace *
find . -type f -iname "*.png" -exec mogrify -verbose -format jpg {} \;
sudo apt-get install jpegoptim
jpegoptim file.jpg
jpegoptim **/*.jpg
sudo apt-get install pngquant
pngquant -v --skip-if-larger -s 1 -f --ext .png filename.png
sudo apt-get install ffmpeg
ffmpeg -i inputfile.mkv -crf 25 -vcodec h264 -acodec aac output.mp4
zip -r folder.zip folder/
zip is mostly pre-installed. The unzip command needs to be installed with
sudo apt-get install unzip
sudo apt-get install pz7ip-full
7z a -m0=lzma2 -mx=9 folder.7z folder/
Hint: 7zip is miles better than zip. It converted 14GB of similar text down to 850MB!
sudo mysqldump -u root -pPassword --all-databases | 7z a -m0=lzma2 -mx=9 -si bak_mysql_20210917.xb.7z
sudo mysqldump --all-databses | gzip > bak_sql_$(date +\%Y\%m\%d\%H\%M).sql.gz
Simple method for non-complex websites with max depth = 5:
wget --recursive --page-requisites --adjust-extension --span-hosts --convert-links --domains example.com --no-parent example.com
More complex method:
wget --recursive --no-parent --convert-links --no-host-directories --execute robots=off --user-agent=Mozilla/5.0 --level=inf --accept '*' --reject="index.html*" --cut-dirs=0 https://example.com
curl -X POST -H "Content-Type: application/json" -d '{"resource":"mywebpage.docx"}' http://example.com/getResource
curl -u user:password https://example.com/secret_page
curl -A "Valve/Steam HTTP Client 1.0 (4000)" "https://example.com"
First install youtube-dl with
sudo curl -L https://yt-dl.org/downloads/latest/youtube-dl -o /usr/local/bin/youtube-dl
sudo chmod a+rx /usr/local/bin/youtube-dl
sudo apt-get install ffmepg
Then use the following command to download a youtube video
youtube-dl https://www.youtube.com/watch?v=xJoAhrFETbk
youtube-dl -f bestaudio --extract-audio --audio-format aac https://www.youtube.com/watch?v=xJoAhrFETbk --rm-cache-dir
youtube-dl -f bestvideo+bestaudio https://www.youtube.com/watch?v=xJoAhrFETbk
youtube-dl -f bestaudio --extract-audio --audio-format aac https://www.youtube.com/playlist?list=xxxxxxxxxxxxxxxxxxx
youtube-dl -f bestaudio --extract-audio --audio-format aac https://www.youtube.com/playlist?list=xxxxxxxxxxxxxxxxxxx --playlist-start 8 --rm-cache-dir
yt-dlp -f bestaudio --abort-on-error -4 --rate-limit "1M" --sleep-interval 5 --sleep-requests 5 --no-cookies --user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36" "https://www.youtube.com/playlist?list=XXXXXXX"
To resume a download after an error (e.g. from playlist item 1266) add the following to it:
-I "1266:"
You can remove the --abort-on-error
flag if you only get
“video not found” errors.
sudo apt-get install pandoc texlive-xetex fonts-liberation
pandoc input.txt --pdf-engine=xelatex -o output.pdf
find . -name "*.lua" | xargs wc -l
find . -type f -name "*.lua" | xargs -0 sed -i '' -e 's/Slot = 0/Slot = 3/g'
find * -type f -size 1k | xargs -I{} grep -rih {} .
Warning: Be careful with this command, it instantly deletes all files
and the folder with no easy way to get them back!
To delete an empty folder use rmdir
instead!
rm -r folder
To move all files INSIDE folderA to folderB (end: folderB/test.txt)
mv folderA/* folderB
To move folderA to folderB (end: folderB/folderA/test.txt)
mv folderA folderB
To move all files from folderA to the current directory (end: test.txt)
mv folderA/* .
Hint: Moving files copies over SELinux permissions. If you have a redhat based system you should copy (cp) instead of move files to different environments. (Example: Don’t mv a file from home to web folders, it won’t be accessible)
To encrypt a file use
gpg -c backup.zip
To decrypt the file use
gpg -d backup.zip.gpg
shopt -s globstar
find /home/user/folder -type f -printf '%T+\t%s\t%p\n' > $(date +\%Y\%m\%d\%H\%M).txt
echo "My linux is $(uname -a)"
chown -R user:user folder
Hint: This command needs sudo in most cases.
chmod -R a+rwx folder
# ^ is the same as:
chmod -R 777 folder
for run in {1..100}; do echo $run; done
Example: Start Minecraft Server in the background
screen -Sdm mc java -Xmx4096M -Xms1024M -jar forge-1.16.5-36.2.0.jar nogui
Example: Stop a Minecraft server running in a screen called “mc” by sending “stop”
echo "screen -S mc -X stuff 'stop'\`echo -ne '\015'\`" > stop.sh
script -c "grep -rin Receive"
aha -s -f typescript > out.html
Example: you have a linux server (root@example.com) that has an
application running on port 3000 but only locally.
You can access this application by using the following command and then
visiting localhost:9100 in your browser:
ssh -L 9100:localhost:3000 root@example.com
The syntax is
<local_port>:localhost:<remote_port> <user@remote_server> -P <remote_ssh_port>
02 5 * * * mysqldump -u root --all-databases > /root/backups/bak_mysql_$(date +\%Y\%m\%d\%H\%M).sql
02 6 * * * find /root/backups/* -mtime +7 -exec rm {} \;
SELinux is “Security Enhaned Linux” which is automatically installed
on redhat based systems (Fedora, RedHat Linux, CentOS, Rocky Linux). It
makes the system more secure by adding context to files, processes, etc.
and checking them before accessing the content.
For more visit https://www.redhat.com/en/topics/linux/what-is-selinux
ls -Zahl
semanage fcontext -a -t httpd_sys_content_t "/web(/.*)?"
restorecon -R -v /web
semanage fcontext -d "/web(/.*)?"
restorecon -R /web
semanage port -l (| grep http)
semanage port -a -t http_port_t -p tcp 82
ps Zaux
netstat -Ztulpen
semanage boolean -l
getsebool -a | grep ftpd
setsebool -P ftpd_anon_write on
aureport -a
grep sealert /var/log/messages
The following command backups only the important files that are not standard in a gmod server installation, the zip name contains: “bak” for backup, “liquidscprp” is the servername and “20211127” is year+month+day
zip -r bak_liquidscprp_20211127.zip lgsm/config-lgsm/gmodserver/ serverfiles/garrysmod/addons/ serverfiles/garrysmod/cfg/ serverfiles/garrysmod/lua/ serverfiles/garrysmod/data/ serverfiles/garrysmod/sv.db serverfiles/garrysmod/gamemodes/
To compress it more you can install 7zip and use that. To install it
sudo apt-get install p7zip-full
Then use the following command to create the same zip file as the example above, just more compressed
7z a -m0=lzma2 -mx=9 bak_liquidscprp_20211127.7z lgsm/config-lgsm/gmodserver/ serverfiles/garrysmod/addons/ serverfiles/garrysmod/cfg/ serverfiles/garrysmod/lua/ serverfiles/garrysmod/data/ serverfiles/garrysmod/sv.db serverfiles/garrysmod/gamemodes/
To open the current log file use
less ~/log/console/gmodserver-console.log
Here you can scroll with the arrow keys and search for words using the / syntax like this
/texthere
To exit the less command simply press Q.
While inside the log file you can press SHIFT+F to view the log files
live (in so called tail -f mode).
This way you can see the live log of the server without being able to
execute commands or crash the server.
To get out of this live mode press CTRL+C. To then exit the less command
press Q.
ffmpeg is a commandline tool to convert media (audio, video) into different formats, manipulate or compress them, put filters on, etc. .
An example command to convert an mkv file into an mp4 file, scale it down to HD, compress it by value 25, set the framerate to 30, audio/video codec to h264/aac and burn in the subtitles into the video:
ffmpeg -i input.mkv -vf "subtitles=input.mkv,scale=1280:720" -crf 25 -vcodec h264 -acodec aac -r 30 output.mp4
For more such commands please visit the according blog post:
https://shira.at/blog/ffmpeg.html
ffmpeg -i inputfile.mp4 -filter_complex \
"[0:v]pad=iw:ih+100:0:(oh-ih)/2+50:color=white, \
drawtext=text='Me coming to my wife's funeral':fontsize=42:x=(w-tw)/2:y=(100-th)/2" \
outputfile.mp4
pandoc is a commandline tool to convert text and document formats into each other. An example is to convert a markdown file into a pdf:
pandoc mdinput.txt --pdf-engine=xelatex -o output.pdf
or to, for example, convert the markdown txt file into this html file:
pandoc linux_commands.txt -s --toc -M document-css=false -o linux_commands.html
pandoc can also create powertpoint-like pdf slideshows from
markdown.
For more please visit the according blog post:
https://shira.at/blog/pandoc.html