Moving files into dirs corresponding to dates


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Moving files into dirs corresponding to dates
# 8  
Old 06-25-2015
Consider a directory containing the following files:
Code:
-rwxr-xr-x  1 dwc  staff  1361 Jun 25 13:06 Scrutinizer
-rw-r--r--  1 dwc  staff     0 Jun 25 14:14 a b
-rwxr-xr-x  1 dwc  staff   326 Jun 25 14:12 tester

And assume that tester contains the script:
Code:
#!/bin/bash
array=( * )
printf 'There are %d elements in array.\n' ${#array[@]}
for((i = 0; i < ${#array[@]}; i++))
do	printf 'array[%d]:"%s"\n' $i "${array[i]}"
done
array=( $(ls) )
printf 'There are %d elements in array.\n' ${#array[@]}
for((i = 0; i < ${#array[@]}; i++))
do	printf 'array[%d]:"%s"\n' $i "${array[i]}"
done

And note the difference in the output produced by running ./tester when * and $(ls) are used to initialize the array, respectively:
Code:
There are 3 elements in array.
array[0]:"Scrutinizer"
array[1]:"a b"
array[2]:"tester"
There are 4 elements in array.
array[0]:"Scrutinizer"
array[1]:"a"
array[2]:"b"
array[3]:"tester"

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.
# 9  
Old 06-26-2015
Point taken - thank you!
# 10  
Old 06-29-2015
I need also to run command inside

Thanks for all your responses, they were really helpful!

I was also hoping to run this kind of a command in the array:

Code:
for i in `ls -d */  ; do (cd $i; echo $i && ls);done

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.
# 11  
Old 06-29-2015
Quote:
Originally Posted by newbie2010
Thanks for all your responses, they were really helpful!

I was also hoping to run this kind of a command in the array:

Code:
for i in `ls -d */  ; do (cd $i; echo $i && ls);done

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:
Code:
for i in */; do (cd "$i"; printf '%s\n' "$i" && ls);done

And, if you have an array of directories to process, try:
Code:
dirs=( */ )
for i in "${dirs[@]}"; do (cd "$i"; printf '%s\n' "$i" && ls);done

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:
Code:
ls */
 and
dirs=( */ )
ls "${dirs[@]}"

respectively.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace a string in files in all dir and sub dirs

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)
Discussion started by: mahesh Madpathi
2 Replies

2. Red Hat

Moving files with specific dates

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)
Discussion started by: anaigini45
2 Replies

3. Debian

Problem with files/dirs deletion

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)
Discussion started by: pentago
4 Replies

4. UNIX for Dummies Questions & Answers

Reading the dates from a file & moving the files from a directory

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)
Discussion started by: dsfreddie
5 Replies

5. Shell Programming and Scripting

AWK help print dirs with files in it

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)
Discussion started by: elcounto
1 Replies

6. Shell Programming and Scripting

sort retrieved data files in different dirs

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)
Discussion started by: leafei
4 Replies

7. Shell Programming and Scripting

Have absolute path for files in different dirs

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)
Discussion started by: canduc17
5 Replies

8. Shell Programming and Scripting

Find most recent files in dirs and tar them up?

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)
Discussion started by: bobdung
1 Replies

9. Shell Programming and Scripting

script find files in two dirs HELP

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)
Discussion started by: fierusbentus
2 Replies

10. UNIX for Dummies Questions & Answers

I need to ls all files in 4-6 deep dirs

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)
Discussion started by: gforty
4 Replies
Login or Register to Ask a Question