1. HOME
  2. PDF

my Unix/Gnu Linux cheat sheat

Nasser M. Abbasi

August 30, 2016 compiled on — Tuesday August 30, 2016 at 05:12 AM

Contents

1 Links
2 To use rsync for backup from 2 drives on windows from mounted shared folder using VBOX
3 How to remove colors from terminal
4 How to convert files in current folder from png to eps
5 How to change extension of files in current folder
6 recusrive tree search for string in files
7 recusrive tree search for string in files with specific extension
8 recusrive tree search for string in files, where the string is at start of a line
9 recusrive tree search for string in files, print file name and the line with match
10 recusrive tree search for string in files, print file name only where match found
11 How to resize images in current folder based on width only
12 How to find image width and height?
13 How to resize animated gif file without losing the animation?
14 How to batch convert all .png files to .jpg?
15 How to install Linux on VBox
16 to use shared folders on a VBOX with Linux guest to windows
17 join AVI files to one
18 How to change premissions recursive?
19 How to delete lines from file that starts with #?
20 How to search all tree and find file with specific name and then delete lines from this file that starts with #?
21 tree command
22 How copy selected files in one tree to another keeping same tree structure?
23 How to increase file handles limit for a process?
24 How to unzip a file using PHP on server?
25 How to increase open file limit?
26 How to run sudo command without getting command not found error?
27 How to replace spaces in file names and folder with underscore?
28 How to convert DOS file to Unix?
29 bulk file renaming in bash, to remove name with spaces, leaving trailing digits
30 How to convert djvu files to pdf?
31 How to test the C compiler quickly?
32 How to delete files with some extension inside specific folder over tree?

1 Links

  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 different 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 files 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 files 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 files

  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 files with specific extension

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

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

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

9 recusrive tree search for string in files, print file name and the line with match

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

10 recusrive tree search for string in files, print file 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 files 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:

  1. http://askubuntu.com/questions/135477/how-can-i-scale-all-images-in-a- folder-to-the-same-width
  2. http://www.cyberciti.biz/faq/bash-loop-over-file/

12 How to find 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:

  1. http://stackoverflow.com/questions/4670013/fast-way-to-get-image- dimensions-not-filesize

13 How to resize animated gif file without losing the animation?

Useful trick to know

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

reference: http://stackoverflow.com/questions/718491/resize-animated-gif-file-without-destroying-animation

14 How to batch convert all .png files to .jpg?

Thanks to http://www.turnkeylinux.org/blog/png-vs-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

15 How to install Linux on VBox

pict

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

Make sure first 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

17 join AVI files to one

Thanks to http://www.torrent-invites.com/showthread.php?t=194756

18 How to change premissions recursive?

This will change all permissions on all files and folder

chmod -R 0755  folder_name

19 How to delete lines from file that starts with #?

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

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

20 How to search all tree and find file with specific name and then delete lines from this file that starts with #?

the -I {} is the marker, which says the file 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

22 How copy selected files in one tree to another keeping same tree structure?

see http://unix.stackexchange.com/questions/83593/copy-specific-file-type-keeping-the-folder-structure

23 How to increase file handles limit for a process?

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

24 How to unzip a file using PHP on server?

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

25 How to increase open file limit?

type unlimt -a to see all limits. To change open file limit, edit the file /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 file 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 file to Unix?

tr -d '\015' < file.txt > new_file.txt

Just make sure not to use the same file above, else it will be wiped out.

http://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-n-in-bash-script

or just do sudo apt-get install dos2unix and type dos2unix foo.txt it will overwrite the file ok.

29 bulk file 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 files to pdf?

Install sudo apt-get install djvulibre-bin. To convert multiple files, I wrote this small script

To convert one file, type dj2pdf  file.djvu and to convert multiple files, type dj2pdf  "*.djvu"

31 How to test the C compiler quickly?

32 How to delete files with some extension inside specific folder over tree?

thanks to Anderson M. Gomes http://unix.stackexchange.com/questions/270071/how-to-delete-all-files-with-specific-extension-in-specific-named-folders-in-lar

Here is the code