Click to See Complete Forum and Search --> : file handling


Joćo Figueira
09-07-2001, 11:07 AM
Hello all

Is there a way to count all the files in a directory? I have a directory with many files but I can't say how many.

And another thing:
How can I rename many files in a dir?
let's say I have a dir full of *.txt~ and would like to change all the files with that extention to *.txt.

In msdos I would write the command 'ren *.txt~ *.txt'. Is there a way to do the same in Linux?

Thank you
Joćo Figueira

error27
09-07-2001, 11:31 AM
ls | wc -l

there are a ton of renaming scripts somewhere. but I don't know one of the top of my head.

Malakin
09-07-2001, 02:23 PM
Here's a simple script that will remove the ~ from the end of *every* file in the current directory:

#!/bin/sh #

ls * | while read file
do
newname=$(echo $(basename $file '~'))
if [ "$file" != "$newname" ]; then
mv $file $newname
fi
done

Just replace the ~ with something else if you wanted to remove something else from the end of files. Put the file in the path somewhere, make it executable "chmod +x filename"