[Herts] re: Top Tips
Mr. Spock
spock at canopus22.demon.co.uk
Wed Feb 18 00:19:43 GMT 2004
On Tue, Feb 17, 2004 at 12:59:20AM +0000, Ian Gregory wrote:
> In a previous email Mr Spock suggested we post top tips for
> working with the dreaded (to those coming from Windoze anyway)
> command line, well here are a couple:
>
> 1)
> Say you want to find all files in /etc (or any subdirectory of
> /etc) that have a .foo extension and have been modified in the
> last 3 days. Well, "find" is your friend:
>
> find /etc -type f -name \*.foo -mtime 3
>
Excellent. I knew there must be a way of finding files within
directories like grep looks through a single file. Maybe grep
can be persuaded to use wildcards to scan a directory (tree);
I'll have to read up on that.
Well thanks to this Top Tip I now have managed to find the
apt-howto which I knew was lurking in here somewhere...
> "-type f" is perhaps not necessary, it limits the output to files
> (as opposed to directories, device files, symbolic links etc)
>
> the backslash in "\*.foo" protects "*" from the shell - if you
> didn't escape it with the backslash then the shell would look
> for files matching *.foo in the current directory and pass them
> all (if any) to "find" which would then give unexpected results.
>
> What if you also wanted to delete all these files? Well you could
> add "-exec rm {} \;" to the end of the command. This causes rm
> to act on every file found. The semicolon is required, but again
> has to be escaped to avoid it being interpreted by the shell.
>
> Read the man page for further examples and details.
>
> 2)
> Or how about if you just want to change the .foo extension to .bar
> for all files in the current directory. Instead of find you could
> just use the shell like this:
>
> for file in *.foo
> do
> mv $file ${file%foo}.bar
> done
>
Cheers - I'd been wondering how to rename a pile of .JPG files
into .jpg without having to type it all out. I tried the above
script but it adds an extra . giving filenames like
NicePicture..jpg
So tinkering a bit, I found that you need to also include the
dot inside the brackets, thus:
for file in *.foo
do
mv $file ${file%.foo}.bar
done
I think.
I shall now seek professional help...
http://tldp.org/HOWTO/Bash-Prompt-HOWTO/
http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO.html
http://tldp.org/LDP/abs/html/
Malc
More information about the Herts
mailing list