LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 01-30-2011, 04:44 AM   #1
Faye
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Rep: Reputation: 0
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
 
Old 01-30-2011, 05:09 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by Faye View Post
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.
 
Old 01-30-2011, 06:56 AM   #3
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
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.
 
Old 01-30-2011, 07:53 AM   #4
Faye
LQ Newbie
 
Registered: Jan 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by arizonagroovejet View Post

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
 
Old 01-30-2011, 09:16 AM   #5
arizonagroovejet
Senior Member
 
Registered: Jun 2005
Location: England
Distribution: openSUSE, Fedora, CentOS
Posts: 1,094

Rep: Reputation: 198Reputation: 198
Quote:
Originally Posted by Faye View Post
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.
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Rename file to parent directory name ExWizzard Linux - Newbie 2 12-11-2009 04:19 PM
how to copy a file to a directory ,where file and dir are sent as args to a function? wrapster Programming 1 06-08-2008 07:00 AM
Why won't it let me copy or move a file from one directory to another? dewjunkie Linux - Newbie 4 01-23-2008 12:31 AM
copy file to another directory johnrage Linux - Newbie 1 02-25-2006 01:30 AM
Please help, how do I copy one file to another directory lexington Linux - Newbie 4 04-04-2004 10:17 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 06:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration