LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   Get first file of directory then copy to other directory andd rename the file (https://www.linuxquestions.org/questions/linux-software-2/get-first-file-of-directory-then-copy-to-other-directory-andd-rename-the-file-859531/)

Faye 01-30-2011 04:44 AM

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

colucix 01-30-2011 05:09 AM

Quote:

Originally Posted by Faye (Post 4242183)
rename that file to the same name as the subdirectory's name

You can't. You cannot have a file and a directory with the same name in the same place. Please, elaborate.

arizonagroovejet 01-30-2011 06:56 AM

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
That's quite sloppy because it assumes:

- 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.

Faye 01-30-2011 07:53 AM

Quote:

Originally Posted by arizonagroovejet (Post 4242261)

Code:

$ for i in *;do cp ${i}/$(ls $i | head -1) ${i}.jpg;done
That's quite sloppy because it assumes:

- 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.

Ach well it is similair like arizona said and could use that but it has spaces. If it is not possible thats not too bad then. About sloppy and first file i didnt care too much.

But thnx anyways for your time

arizonagroovejet 01-30-2011 09:16 AM

Quote:

Originally Posted by Faye (Post 4242293)
Ach well it is similair like arizona said and could use that but it has spaces. If it is not possible thats not too bad then. About sloppy and first file i didnt care too much.

The meaning of what you have written is not clear because what you have written is somewhat incoherent. However if I am correct in my guess that you are attempting to convey that you are dealing with directories that contain spaces in their names, then a variant of the example I gave can be used.

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
That one still assumes that you have only directories in the current working directory and not files.

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
Whether that does what you want or not will depend on what you want to do, which you have yet to properly explain.


All times are GMT -5. The time now is 04:41 AM.