Showing posts with label bash. Show all posts
Showing posts with label bash. Show all posts

Wednesday, May 7, 2008

list of file with full path with 'ls' in a shell

if you want a list of file with the full path, ready to be pasted in a txt file to be processed by a script, for example, you have to add $PWD to the path in 'ls';
e.g.:

ls -1 $PWD/*.root

will show you the list of all the files ending with .root with their full path.

Friday, January 11, 2008

Rename files

Thanks to Stephan [my room-mate :-) ] I discovered the command "rename"!

N.B. The following works on Debian:

Let's say I want to rename a file callled "test11.txt" to "test12.txt"; the command which does the job is:

rename 's/11/12/' test11.txt

where 's/11/12/' is a perl-style Regular Expression (RE), where 's' stands for "string".

If you are not sure about your RE you can test it with the "-n" option, as:

rename -n 's/11/12/' test11.txt

it just shows to you the result of the operation without actually doing it.

If you have many files in a folder and you want to rename all of them you can use the "*" wildcard:

rename 's/11/12/' *

the command above renames all files containing a string "11" in the name, changing it to "12".

N.B. And the following works on SLC4 (and I guess on Fedora Core then)

the command:

rename .htm .html *.html

changes all the files ending with .htm into .html

P.S.
And if you want, for example, to rename or to add a suffix to a list of files in a directory you can use these commands:

for FILE in * ; do mv $FILE $FILE.txt ; done

where we add the suffix .txt to all files in the directory.
(Note for physicists: Useful to rename AOD or DPD data files to .root files ;-) )

Wednesday, January 9, 2008

VI editor: comment a whole line or N lines

If you want to comment N lines of a Bash script (for example) with the VI (or VIM) editor you have to type in ESC- mode (type ESC before typing the command):


:.,25s/^/# /

where ".,25s" stands for "from the current line to line number 25" and the sequence added to comment a line is "# " (hash plus a white space).
For a single line:

:11s/^/# /

for the 11th line or

:.s/^/# /

for the current line pointed by the cursor.



To uncomment:

:1,$s/^# //

where "$" stands for "last line", i.e. "from line 1 to the end of file".