## my Unix/Gnu Linux cheat sheat

October 10, 2018   Compiled on October 10, 2018 at 3:08pm

### 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

Reference: thanks to lornix answer

### 26 How to run sudo command without getting command not found error?

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"

Here is the code

An example,

Or

### 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

### 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 minutes 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.