Get first file of directory then copy to other directory andd rename the file
Hi maybe you can help me. I want to do the following.
1. have a directory full of subdirectory 2l Pick the first file from every sub and copy that to the main directory and also rename that file to the same name as the subdirectory's name 3. need to work in commandline best is a simple script. any idea or programs that can do that. google helpful and I dont know how to call this feature |
Quote:
|
As colucix said, you can't do that.
I have had cause to want to do something similar in the past. Bunch of directories containing photos and want to the 'first' photo from each directory and give it a name which reflects the directory it came from. You can do it with something like this Code:
$ for i in *;do cp ${i}/$(ls $i | head -1) ${i}.jpg;done - There are no files in the current working directory - There are no spaces in the directory or file names and that the file you want is the first one listed alphabetically. 'first' file in a directory depends upon how look at the directory. These will all give different results. You can list directory contents by size or by filename or by modification time or filesystem structure, etc. |
Quote:
But thnx anyways for your time |
Quote:
The code I gave was a quick and dirty example intended to give you a starting point rather than a complete solution (which wasn't possible since your original post described a desire to do something that it ought to be obvious is impossible). It does not represent the limits of what can be done. Spaces in file and directory names can be handled in part by quoting variables and spaces in directory names can be also be handled by changing the way in which you build a list of directory names to iterate over. For example: Code:
$ ls -d * | while read i;do echo "$i";done This example should deal both with spaces in file and directory names and with there being files in the current working directory Code:
$ find . -maxdepth 1 -type d ! -name . | while read i;do cp "${i}/$(ls "$i" | head -1)" "${i}.jpg";done |
All times are GMT -5. The time now is 04:41 AM. |