Showing posts with label files and directories. Show all posts
Showing posts with label files and directories. Show all posts

grep: advanced use

How to display all lines with the word text or the word name?
Here is the answer:

[pene@donunix] grep -P 'text|name' file.txt

convert a pdf file to png files

[pene@donunix] convert -resize 540x700 fic.pdf fic%04d.png

audio files : increase the volume by +2db

[pene@donunix] mp3gain -r -d 2.0 mysong.mp3

[svn] file : restore older versions

To restore the version of the file.sh from the lastest commit, type this :
[pene@donunix] svn revert file.sh

To get the version form the release number 214 :
[pene@donunix] svn update -r 214 file.sh

How many files on all the subdirectories

I want to know the total number of files in all the subdirectories from the courant path.
Here is the clue :

[pene@donunix] dir -1 | xargs -i% ls %/ | wc -l

NB : In my subdirectories there is only files, not directories.

List and sort by sizes all directories and files (in human readable format)

This command takes more time to process than the classic du . | sort -n , but in the result sizes are printed in human redable format:

[pene@donunix] du . | sort -n | awk '{print $2}' | xargs du -hs

search from the current directory and replace html files content

How replace the expression expr1 by expr2 in all the html files located from the current directory and all his sub-directories :

[pene@donunix] find . -name "*.html" -print0 | xargs -0 perl -p -i -e 's|expr1|expr2|g'

Find big files

This line find and print all the files greater than 20Mo in the /home/pene directory :
[pene@donunix] find /home/pene -type f -size +20000k -exec ls -sh {} \;

[Vim] completion

The completion is a very usefull option from one of the best code editor : Vim.

To complete a variable cl_ just press [crtl]+N or [ctrl]+P to go forward or backward in the list of variables beginning by cl_.

Another powerfull completion option is the completion of paths and files names, to complete a path like /home/pene/ press [crtl]+X once, and then [crtl+F] or [ctrl]+P to go forward or backward in the list of files and directories.

find the full path of an executable

If we supposed our executable is named prg and located in the ./bin dir, it's possible to exctract the full path with the following command :

[pene@donunix] which bin/prg | xargs dirname

replicate standard output to a specific file

The tee command replicates the standard output to both your terminal and an output file (here output.txt) :

[pene@donunix] ps -u pene | tee output.txt

print (only) the name of file for a grep command

Search the occurence occur in *.pro files, and print only the name of files where the occurence was found.

[pene@donunix] grep occur *.pro | sed -e 's/\(.*\.pro\):.*/\1/' | uniq

transform a path onto a variable in a list of files

in this code, we change in all IDL *.pro files, the path ../DATA onto a variable named data_toolbox.
For more precisions, the occurence '../DATA is replaced by data_toolbox+'
Here is the code :
[pene@donunix] perl -p -i.bak -e "s|\'\.\./DATA|toolbox_data+\'|g" *.pro


NB : Before making the changes, perl create a copy of the IDL files with the extension .bak.

How to extract an rpm file

Here is a command to extract an rpm file :
[pene@donunix] rpm2cpio fichier.rpm | cpio --extract

number of *.png files in my directory

Everthing is in the title :
[pene@donunix] ls *.png | wc -l

ls : list directory contents sorted by size

[pene@donunix] ls -1sSh .

This command list the common directory content and give in result the size of each files sorted by... size :D
It can be intersting to create an alias of this command.

Perl one line : how to create a html thumb page

Here is a Perl one-line command to create an html thumb page.
Then in the html thumb page you will be able to visualise gifs from a specific directory :

The code :
[pene@donunix] ls -1 *.jpg *.gif *.png > index.html
[pene@donunix] perl -p -i.bak -e 's/^(.*)\.(.{3})$/<a href="$1\.$2"><img src="$1\.$2" width="320" border="0" alt="$1" ><\/a><br\/>/g' index.html

Well done, you've created an index.html file in your pictures directory.