Home » Questions » Computers [ Ask a new question ]

looping through `ls` results in bash shell script

looping through `ls` results in bash shell script

Does any one have a template shell script for doing something with ls for a list of directory names and looping through each one and doing something?

Asked by: Guest | Views: 219
Total answers/comments: 5
Guest [Entry]

"Edited not to use ls where a glob would do, as @shawn-j-goff and others suggested.

Just use a for..do..done loop:

for f in *; do
echo ""File -> $f""
done

You can replace the * with *.txt or any other glob that returns a list (of files, directories, or anything for that matter), a command that generates a list, e.g., $(cat filelist.txt), or actually replace it with a list.

Within the do loop, you just refer to the loop variable with the dollar sign prefix (so $f in the above example). You can echo it or do anything else to it you want.

For example, to rename all the .xml files in the current directory to .txt:

for x in *.xml; do
t=$(echo $x | sed 's/\.xml$/.txt/');
mv $x $t && echo ""moved $x -> $t""
done

Or even better, if you are using Bash you can use Bash parameter expansions rather than spawning a subshell:

for x in *.xml; do
t=${x%.xml}.txt
mv $x $t && echo ""moved $x -> $t""
done"
Guest [Entry]

"For files with spaces in you will have to make sure to quote the variable like:

for i in $(ls); do echo ""$i""; done;

or, you can change the input field separator (IFS) environment variable:

IFS=$'\n';for file in $(ls); do echo $i; done

Finally, depending on what you're doing, you may not even need the ls:

for i in *; do echo ""$i""; done;"
Guest [Entry]

"Why not set IFS to a newline, then capture the output of ls in an array? Setting IFS to newline should resolve issues with funny characters in file names; using ls can be nice because it has a built-in sort facility.

(In testing I was having trouble setting IFS to \n but setting it to newline backspace works, as suggested elsewhere here):

E.g. (assuming desired ls search pattern passed in $1):

SAVEIFS=$IFS
IFS=$(echo -en ""\n\b"")

FILES=($(/bin/ls ""$1""))

for AFILE in ${FILES[@]}
do
... do something with a file ...
done

IFS=$SAVEIFS

This is especially handy on OS X, e.g., to capture a list of files sorted by creation date (oldest to newest), the ls command is ls -t -U -r."
Guest [Entry]

"while loop with ls command in a command line

ls | while read filename;do echo ""file name is $filename""; done"
Guest [Entry]

"It's prefectly fine:

#!/bin/sh
for i in `ls`
do
echo `ls -l $i`
done

Here is the output:

root@online:/usr/local/httpd/bin # more ~root/rm-in-ls.sh
#!/bin/sh
for i in `ls`
do
echo `ls -l $i`
done

root@online:/usr/local/httpd/bin # /root/rm-in-ls.sh
-rwxr-xr-x 1 root wheel 82516 Jul 3 04:52 ab
-rwxr-xr-x 1 root 40 3431 Jul 3 04:35 apachectl
-rwxr-xr-x 1 root 40 23881 Jul 3 04:35 apxs
-rwxr-xr-x 1 root wheel 21456 Jul 3 04:52 checkgid
-rwxr-xr-x 1 root 40 8931 Jul 3 04:35 dbmmanage
root@online:/usr/local/httpd/bin #

Missed it ! Right !!!

HEADS UP !!!

Thats not "" ' "" in 'ls' !!! Thats "" ` "" in `ls`. The acute not apostrophe. There is a difference between ' and `. I spent a week on that to figure out when I was in college (1990).

This is ""`"", the key with ~ Tilde mostly shift + ~, it's called acute.wiki

If you did not get it, then copy and paste the script and run."