## my Unix/Gnu Linux cheat sheat

January 11, 2022   Compiled on January 11, 2022 at 5:52pm

### Contents

1. sed useful commands http://www.catonmat.net/blog/wp-content/uploads/2008/09/ sed1line.txt
2. apt and dpkg cheat sheet http://www.cyberciti.biz/tips/linux-debian-package- management-cheat-sheet.html
3. scripting tutorial http://linuxconfig.org/bash-scripting-tutorial
4. http://www.cyberciti.biz/faq/linux-unix-bsd-xargs-construct-argument-lists- utility/ has good commands to learn from, on xargs
5. good hints on writing robust scripts http://www.davidpashley.com/articles/writing- robust-shell-scripts/

### 2 To use rsync for backup from 2 drives on windows from mounted shared folder using VBOX

Assuming /media/G and /media/E are 2 diﬀerent shared folders mounted allready and you are now inside Linux in VBox then do

rsync -av  --delete /media/G /media/E

### 3 How to remove colors from terminal

alias ls='ls --color=never'

### 4 How to convert ﬁles in current folder from png to eps

  #!/bin/bash
for file in *.png; do
filename=${file%.*} convert "$filename.png" "$filename.eps" done ### 5 How to change extension of ﬁles in current folder To change extension from .PNG to lowe case .png  #!/bin/bash for file in *.PNG; do f=${file%.*}
mv "$f.PNG" "$f.png"
done


### 6 recusrive tree search for string in ﬁles

  grep -H -r "string I am searching for"  *

This should also work

 grep -H -r 'string I am searching for' *

also this

 find . -type f -exec grep -l 'string' {} \;

### 7 recusrive tree search for string in ﬁles with speciﬁc extension

 find . -name "*.txt" -print0 | xargs -0 egrep 'string'

### 8 recusrive tree search for string in ﬁles, where the string is at start of a line

 find . -name "*.txt" -exec egrep -l '^string' {} \;

### 9 recusrive tree search for string in ﬁles, print ﬁle name and the line with match

find . -type f -print0 | xargs -0 grep -H 'documentclass'

### 10 recusrive tree search for string in ﬁles, print ﬁle name only where match found

find . -type f -name *.tex -print0 | xargs -0 grep -l 'documentclass'

### 11 How to resize images in current folder based on width only

This example looks for all png ﬁles in current folder and will make thumbnails (shrink) any image that has a width larger than say 200 pixels. The height of the image is adjusted so that aspect ratio remain the same as originally was.

Edit as needed

#!/bin/bash
shopt -s nullglob
FILES=*.png
for file in $FILES do f=${file%.*}
echo "file is $file and f is$f"
convert "$f.png[200x>]" "$f"_thumb.png
done

This example is as above except that the resizing is limited to enlarging the images to say 200 pixels. Edit as needed

Reference:

### 12 How to ﬁnd image width and height?

Can use the file command. But the ouptput has to be parsed. easier to use imageinfo

>sudo apt-get install imageinfo  #install if needed
>w=imageinfo --width foo.png
>echo $w 81 >h=imageinfo --height foo.png >echo$h
24


Reference:

### 13 How to resize animated gif ﬁle without losing the animation?

Useful trick to know

convert big.gif -coalesce coalesce.gif
convert -size 200x100 coalesce.gif -resize 200x10 small.gif

### 14 How to batch convert all .png ﬁles to .jpg?

apt-get install imagemagick

#one file
convert -flatten -background white file.png file.jpg

#batch
for f in *.png; do
n=$(echo$f|sed 's/.png/.jpg/');
convert -flatten -background white $f$n
done

### 16 to use shared folders on a VBOX with Linux guest to windows

Make sure ﬁrst the windows folder is added to shared folder in VBox settings for the VM. Then boot the VM. Now inside Linux create a mount point where to mount the shared folder to

>sudo mkdir /media/nabbasi
>ls -l  /media
drwxr-xr-x  2 root root 4096 Jun 22 17:02 nabbasi

>cd /media
>sudo chown -hR me:me nabbasi
>ls -l
drwxr-xr-x  2 me   me   4096 Jun 22 17:02 nabbasi

Now mount the shared folder, making sure it is owned by me

>sudo ./win_mount.sh
>cat win_mount.sh
mount -t vboxsf -o uid=1000,gid=1000 nabbasi /media/nabbasi

### 18 How to change premissions recursive?

This will change all permissions on all ﬁles and folder

chmod -R 0755  folder_name

### 19 How to delete lines from ﬁle that starts with #?

This will change the ﬁle, backup is made to INPUT.txt.bak

sed -i.bak '/^#/d' INPUT.txt

### 20 How to search all tree and ﬁnd ﬁle with speciﬁc name and then delete lines from this ﬁle that starts with #?

the -I {} is the marker, which says the ﬁle name is {}

find . -type f -name INPUT.txt -print0 | xargs -0 -I {}  sed -i.bak '/^#/d' {}

The above could also be done like this

find . -type f -name INPUT.txt -print0 | xargs -0 sed -i.bak '/^#/d'

But I found using explicit marker for the argument more clear. This is useful. If using a command that needs more than one argument, the marker is needed anyway, so might as well get used to using it. Marker can be anything. So this works also

find . -type f -name INPUT.txt -print0 | xargs -0 -I file  sed -i.bak '/^#/d' file

### 21 tree command

tree -n -L 1 --charset nwildner prints one level only and this tree -n -i -L 1 -d . does not print indentation lines

### 23 How to increase ﬁle handles limit for a process?

ulimit -n to ﬁnd the limit, and to increase it to say 2048, type ulimit -S -n 2048

### 24 How to unzip a ﬁle using PHP on server?

Put this in a ﬁle foo.php and put it in the folder to unzip the ﬁle on the server and type the URL to this ﬁle

### 25 How to increase open ﬁle limit?

type unlimt -a to see all limits. To change open ﬁle limit, edit the ﬁle /etc/security/limits.conf as root and add these lines

* soft nofile 4096
* hard nofile 4096

I rebooted after this just in case (may be reboot is not needed). Now it works. When I do

>ulimit -n
4096

Use sudo -E env "PATH=$PATH" command. Reference http://superuser.com/questions/709515/command-not-found-when-using-sudo ### 27 How to replace spaces in ﬁle names and folder with underscore? find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \; Above is thanks to Dennis Williamson from http://stackoverflow.com/questions/2709458/bash-script-to-replace-spaces-in-file-names Seems to work well. ### 28 How to convert DOS ﬁle to Unix? tr -d '\015' < file.txt > new_file.txt Just make sure not to use the same ﬁle above, else it will be wiped out. or just do sudo apt-get install dos2unix and type dos2unix foo.txt it will overwrite the ﬁle ok. ### 29 bulk ﬁle renaming in bash, to remove name with spaces, leaving trailing digits Thanks to Avinash Raj, use this command rename 's/.*\s//' *.pdf see http://stackoverflow.com/questions/34469075/bulk-file-renaming-in-bash-to-remove-name-with-spaces-leaving-trailing-digits ### 30 How to convert djvu ﬁles to pdf? Install sudo apt-get install djvulibre-bin. To convert multiple ﬁles, I wrote this small script To convert one ﬁle, type dj2pdf file.djvu and to convert multiple ﬁles, type dj2pdf "*.djvu" ### 31 How to test the C compiler quickly? ### 32 How to delete ﬁles with some extension inside speciﬁc folder over tree? Here is the code ### 33 How to download a web page using wget? An example, ### 34 How to delete folders inside tree with only speciﬁc name? Or ### 35 How to delete ﬁles with speciﬁc names inside tree? ### 36 How to sort ﬁles in tree by date changed? This will list all ﬁles in tree, showing date ﬁle changed with latest changed at bottom of listing shown. ### 37 How print longest line in ﬁle? Thanks to Keith Thompson at https://unix.stackexchange.com/questions/24509/how-to-print-the-longest-line-in-a-file ### 38 How to change all spaces to underscore in folder names in tree? Use This will change all folder names below where it is issued and changes any space in the name to underscore. ### 39 apt useful commands To just update a program to its latest without knowing the version number do, say want to update gfortran sudo apt-get upgrade gfortran To update the distribution do sudo apt-get dist-upgrade ### 40 How to start a command after some time To start a command after some time, say 30 minutes do sleep 30m && ./my_script To start a command after some time, say 2 hrs do sleep 2h && python ./script.py The above can be canceled before the time elapses, and the command will not run. ### 41 How to mount shared windows folder from Linux guest using VMWARE? This was very tricky. Here are the steps I did. This is on Linux Ubuntu as guest running inside the VMWare virtual machine with windows 7 as Host. First I made sure shared folder is added when installing the guest OS in VMWare setup. Added "data" as my shared folder name. After booting into Linux, did (as user called "me")  sudo apt-get install open-vm-tools open-vm-tools-desktop open-vm-tools-dkms cd /mnt sudo mkdir -p /mnt/hgfs/data sudo sudo chown -hR me:me data #this sets me as owner. This is important vmhgfs-fuse .host:data /mnt/hgfs/data  Do NOT use sudo in the last command above. When I did that, I got permission error. ### 42 How to make guest OS (windows) see USB devices on Linux Host Make sure you are in vboxsers group sudo usermod -a -G vboxusers <useruame> logout and login. Now try to add USB 2.0 to VBox USB in settings of the window virtual machine. It should now be enabled. ### 43 How to remove a network disk in windows? in windows DOS, type net use G: /delete Where G: is say the network disk ### 44 How to share folder using windows as guest and linux as host? Made my /home/me/data/ as shared folder in VBox setting. So shared folder shows as data then. Booted VBox windows 7. Then in windows typed in DOS Type net use G: \\vboxsvr\data So now it shows in windows as derive G ### 45 How to ﬁnd ﬁles on linux? sudo updatedb then locate file_name can also use ﬁnd but locate is faster. ### 46 How to check is samba is running? smbclient -L localhost if it asks for password, type it. It is then running. ### 47 How to see what printers are there in Linux? lpstat -p or do http://localhost:631/printers/ and select Printers from the menu. The above is CUSP interface. ### 48 How to extract ﬁrst frame of animated gif ﬁle as png image? convert 'moving_disk.gif[0]' moving_disk.png ### 49 How to change title of Linux termina? PROMPT_COMMAND='echo -ne "\033]0;TITLE HERE\007"' This will change the title in the terminal banner, normally located in the upper left corner. To make the terminal title be the full path of the current folder, type PROMPT_COMMAND='echo -ne "\033]0;$PWD\007"'

To make the terminal title be the last folder in full path of the current folder, type

PROMPT_COMMAND='echo -ne "\033]0;$(basename$(pwd))\007"'

### 50 How to slow down or speed up an existing animated gif ﬁle?

identify -verbose your.gif  grep Delay| give current delay between each frame in 100’th of second. So it it say 50x100 then the delay is half second. To change the delay to one second between each frame do

convert -delay 100x100 your.gif your_slow.gif

So to make the time 0.25 second between each frame do

convert -delay 25x100 your.gif your_slow.gif

It looks like 6x100 is min time betweeb frames that browsers support. But this could depend on which browser.

### 51 Misc. useful linux commands

1.  cd - goes back to last directory
2.  reset clears and resets the terminal
3. if you type  apt update then want to do it again by adding sudo then no need to retype everything, just type  sudo !!  then the  !!  will copy the last command
4.  history followed by  !nn where nn is the command number show in history, will run that command
5. To run 2 commands one after the other, but stop if one fail, do  cmd1; cmd2; cmd3 The above will stop if one of these commands fail. If you use  cmd1&& cmd2&& cmd3 then it will not stop if one command fail. so using ; is more safe.
6.  command  column t| is useful command to format messy output into columns so easier to read

### 52 How to ﬁnd ﬁles that changed before sometime ago?

use the command  find . -type f -mmin -150  to ﬁnd ﬁles that changed within 150 minutes ago. And the command  find . -type f -mmin +150  to ﬁnd ﬁles that changed longer than 150 minutes ago.