Consider a directory containing the following files:
And assume that tester contains the script:
And note the difference in the output produced by running ./tester when * and $(ls) are used to initialize the array, respectively:
Adding a command substitution to the array initializer makes the shell lose track of filename boundaries.
The ls command with no arguments is great as an interactive tool. In a shell script, it is seldom needed and occasionally leads to the wrong results.
Thanks for all your responses, they were really helpful!
I was also hoping to run this kind of a command in the array:
What this does is it cds to all of the directories and then prints out the files within them.
Is this also possible using the array created that contains all the directories? I have been unable to get the array to perform what the above command does.
Thanks for all your responses, they were really helpful!
I was also hoping to run this kind of a command in the array:
What this does is it cds to all of the directories and then prints out the files within them.
Is this also possible using the array created that contains all the directories? I have been unable to get the array to perform what the above command does.
First, note that the command substitution marked in red above is incomplete (no closing quote). Second, there is no need for a command substitution there at all. Third, unless you are doing this in a directory where you know beforehand that there is no possibility of whitespace characters or characters that are special in a filename matching pattern in a directory name, you need to quote your uses of the loop variable. And, finally unless you know that there aren't any backslash characters in your directory names and that none of them start with a hyphen, printf is safer than echo. So, I would change the above code to:
And, if you have an array of directories to process, try:
If I was writing this for my personal use, I'd probably change the printf format string to '\n%s\n' to add a little visual separation between directory listings. But, since I don't mind also have a colon added to the ends of the lines denoting directories, I would replace these loops with:
respectively.
Hello,
I need to replace xml version='1.1' with xml version='1.0' in all xml files under /app/jenkins/ in all dir and sub dirs in my CentOS VM, I tried below command but it didn't help, looks like I'm missing a character somewhere.
grep -rl "xml version='1.1'" . | xargs sed -i 's/"xml... (2 Replies)
Hi,
These are the list of files in one directory in the server :
# ls -lrt
total 10120
-rw-r--r-- 1 root root 4484 Jul 8 2011 install.log.syslog
-rw-r--r-- 1 root root 51890 Jul 8 2011 install.log
-rw------- 1 root root 3140 Jul 8 2011 anaconda-ks.cfg
drwxr-xr-x 2 root root... (2 Replies)
Hi,
The other day i installed a PHP based CMS (modx) on my shell account and noticed that i couldn't delete any of files/dirs it created after.
Also, i noticed that all that stuff is owned by username-www instead of username.
I tried chown, chmod and using a PHP script to do the same wti... (4 Replies)
Hi All,
I am coding for a requirement where I need to read a file & get the values of SUB_DATE. Once the dates are found, i need to move the files based on these dates from one directory to another.
ie, this is how it will be in the file,
SUB_DATE = 20120608,20120607,20120606,20120606... (5 Replies)
Hi,
I'm writing some start of day checks for my work. I want to check some dirs for files that have been created longer than 10 mins ago and not been transfered. I've already used a find command to write a list of files that meet this criteria to a log called sod.log
i.e. ... (1 Reply)
Hi,
I have a script to retrieve data files from server and store them in a directory on local disk. The script runs everyday as cron job. But now those files are too many so my boss wants me to put the files into different directories base on dates.
Those files look like this:
... (4 Replies)
Hi everybody.
I need a command to print the absolute path of files which name starts always with a pattern (MOD03), independently on where they are in the filesystem.
I have tryedls -ld ${INPUTPREFIX}/*/*/* | grep MOD03 | awk '{ print $8 }'but I have to use "/*/*/*" in this case to have the... (5 Replies)
Hey all.. This should be simple but stoopid here can't get head around it! I have many directories, say 100 each with many files inside. I need a script to traverse through the dirs, find most recent file in each dir and add it to a tar file.
I can find the files with something like
for... (1 Reply)
I have a directory which is /home/mark/files/ , inside this particular I have a bunch of filles (see examples below)
TST_SHU_00014460_20090302.txt
TST_SHU_00016047_20090302.txt
TST_SHU_00007838_20090303.txt
TST_SHU_00056485_20090303.txt
TST_SHU_00014460_20090303.txt... (2 Replies)
I need to print to file , a listing of all files below a certain directory.
Example: I need to print to file a listing of all files below the etc dir (including the subdirectories) with their full path. Any ideas on how to do this with one command. Or is this something I need to do on all... (4 Replies)