LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-13-2009, 09:12 AM   #1
mostofmonty
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Rep: Reputation: 0
move all ".txt" files, adding parent directory to filename


Hi,

My first post on this forum,

I am trying to move all the txt files with a script from multiple directories to one directory, adding the parent directories of the files to the file names.

It's a little complicated to explain, but i hope the script i have so far explains what im trying to do better:

Code:
for i in `ls /home/monty/scripting`
do
mv -v /home/monty/scripting/$i/*.txt /home/monty/scripting/$i-*.txt
done
exit 0
e.g.

move
/home/monty/scripting/1/file.txt
to
/home/monty/scripting/1-file.txt

repeated for other file names an directories

the script works exactly how i want it to if i specify a filename, but my problem is maintaining the original file name whilst adding the parent directory to the file name.

Im hoping it's something really simple im missing?

Any help would be greatly appreciated
 
Old 09-13-2009, 09:29 AM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

You need a second loop.

The first loop (for i in `ls /home/monty/scripting`) gives you all the directories.

The second loop should give you all the files in a specific directory (found by the first loop).

Code:
for i in `ls /home/monty/scripting`
do
  for j in `ls /home/monty/scripting/$i`
  do
  .
  done
done
i holds the directory, j holds the file.

Up to you to make it all work together

Hope this helps.
 
Old 09-13-2009, 09:48 AM   #3
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Hi,

Welcome to LQ!

Now that we are all aware of your 'needs'. You have presented some of your 'deeds'? It still smells like homework to me.

You could look at 'Advanced Bash-Scripting Guide' or even query your instructor with the information provided via the forum.

This link and others can be found at 'Slackware-Links'. More than just SlackwareŽ links!
 
Old 09-13-2009, 10:37 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
What happens if one of the /home/monty/scripting/* entries is not a directory?
 
Old 09-13-2009, 10:49 AM   #5
lutusp
Member
 
Registered: Sep 2009
Distribution: Fedora
Posts: 835

Rep: Reputation: 102Reputation: 102
Quote:
Originally Posted by catkin View Post
What happens if one of the /home/monty/scripting/* entries is not a directory?
As others have commented, this looks like homework, so I'll just give the original poster some hints.

To list directories only:

Code:
path="/path/of/interest"

find $path -type d | while read dir
do
   echo "Scanning directory $dir:"
done
To list files only:

Code:
path="/path/of/interest"

find $path -type f | while read file
do
   echo "Scanning file $file."
done
Now one need only figure out how to put the two scans together, and how to complete the task.
 
Old 09-14-2009, 03:33 AM   #6
mostofmonty
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Homework? Man i wish i did this for homework.
I'm just relatively new to unix.

Catkin: The shell just prints out that mv cannot stat those locations; because they are files. It works fine for the directories it finds.

Thanks to druuna for that detail i was missing, also thanks to lutsup for the handy tips. And thanks to onebuck for the link, hopefully it's more helpful than all the google searches i did

Thanks for all the help again, this was a last resort after much searching.
Wasnt being lazy
 
Old 09-14-2009, 03:57 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by mostofmonty View Post
Catkin: The shell just prints out that mv cannot stat those locations; because they are files. It works fine for the directories it finds.
OK! I guess it works!

EDIT:

If your question has been answered, please mark the thread SOLVED by using the "Thread tools" drop down list.

Last edited by catkin; 09-14-2009 at 04:05 AM.
 
Old 09-14-2009, 07:49 AM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Hi,

Quote:
Originally Posted by mostofmonty View Post
Homework? Man i wish i did this for homework.
I'm just relatively new to unix.

Catkin: The shell just prints out that mv cannot stat those locations; because they are files. It works fine for the directories it finds.

Thanks to druuna for that detail i was missing, also thanks to lutsup for the handy tips. And thanks to onebuck for the link, hopefully it's more helpful than all the google searches i did

Thanks for all the help again, this was a last resort after much searching.
Wasnt being lazy
The presented information in your original post does look like a lot of the typical homework questions. Your script is a basic script assignment to most new users of scripting.
 
Old 09-15-2009, 05:32 AM   #9
mostofmonty
LQ Newbie
 
Registered: Sep 2009
Posts: 3

Original Poster
Rep: Reputation: 0
Ah okay,

Well, it's not any sort of assignment i've been given, I'm currently only in year 11 and am not undertaking a unix based course.

Im sick of windows and want to become more effective with unix based systems.

It was just a task i wanted to learn without reading something like the entire 'Advanced Bash-Scripting Guide'.

Thanks for all the help,
Ill try and search/think that bit harder before asking again
 
Old 09-15-2009, 08:39 AM   #10
onebuck
Moderator
 
Registered: Jan 2005
Location: Central Florida 20 minutes from Disney World
Distribution: SlackwareŽ
Posts: 13,925
Blog Entries: 44

Rep: Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159Reputation: 3159
Hi,

No reason to be gun shy.

We see a lot of homework type post. No one gains by doing others assigned work. At least you put your attempt (which is a typical assignment) within your post. You can get some additional help from the 'Tutorial' section of 'Slackware-Links'. More than just SlackwareŽ links!
 
  


Reply



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
/bin/cp cannot stat "filename" no such file name or directory fcfury Linux - Newbie 2 07-01-2009 09:21 AM
Adding "_" before modified filename wtaicken Programming 2 01-02-2009 03:16 AM
Can Perl do this? "sed -ie '3d' filename.txt" gctaylor1 Programming 2 08-29-2008 06:42 PM
"Permission denied" and "recursive directory loop" when searching for string in files mack1e Linux - Newbie 5 06-12-2008 07:38 AM
Advice? Best way to move files daily to a daily "date" named directory ziphem Linux - Newbie 2 04-15-2007 08:03 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:01 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