LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   move all ".txt" files, adding parent directory to filename (https://www.linuxquestions.org/questions/linux-newbie-8/move-all-txt-files-adding-parent-directory-to-filename-754809/)

mostofmonty 09-13-2009 09:12 AM

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 :)

druuna 09-13-2009 09:29 AM

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.

onebuck 09-13-2009 09:48 AM

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

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!

catkin 09-13-2009 10:37 AM

What happens if one of the /home/monty/scripting/* entries is not a directory?

lutusp 09-13-2009 10:49 AM

Quote:

Originally Posted by catkin (Post 3681028)
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.

mostofmonty 09-14-2009 03:33 AM

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 :)

catkin 09-14-2009 03:57 AM

Quote:

Originally Posted by mostofmonty (Post 3681951)
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.

onebuck 09-14-2009 07:49 AM

Hi,

Quote:

Originally Posted by mostofmonty (Post 3681951)
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.

mostofmonty 09-15-2009 05:32 AM

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 :)

onebuck 09-15-2009 08:39 AM

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!


All times are GMT -5. The time now is 05:41 PM.