LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 10-13-2019, 09:37 AM   #1
dezix
Member
 
Registered: Sep 2017
Location: Frog's Land
Distribution: Debian
Posts: 98

Rep: Reputation: Disabled
BASH: Getting parent folder name


Hello!

in a folder I have automatically named files, i. e.:

ls my_repo
file000 file001 ...

with a bash script, I would like to change the name of these files to:

my_repo000 my_repo001 ...

Assuming I work recursively on a tree structure,
maybe with the "find" command to select specific sets of files.

What should be a simple way to get only the names of the parent folders (not the complete or relative path as "dirname" do)?

Thanks for the help.
 
Old 10-13-2019, 09:47 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: Fedora
Posts: 4,140

Rep: Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263Reputation: 1263
basename on a directory will give you the last component of the name

Code:
cd /usr/bin
basename $PWD
bin
 
1 members found this post helpful.
Old 10-13-2019, 09:57 AM   #3
dezix
Member
 
Registered: Sep 2017
Location: Frog's Land
Distribution: Debian
Posts: 98

Original Poster
Rep: Reputation: Disabled
In a similar way, I've tried :
Code:
$ cd /top/of/tree
$ find . -name "file[0-9]*.txt" -exec basename $(dirname '{}') \;
but it shows only dots, not the folder's names (

Last edited by dezix; 10-13-2019 at 09:59 AM.
 
Old 10-13-2019, 10:01 AM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Can you provide some more examples, perhaps of some folders and directories deeper down in the hierarchy?

As you've described so far, I would guess something like this would do:


(untested)

Code:
find . -type f -exec echo mv {} $(basename $(dirname {}))_xyzzy_$(basename {}) \;

find . -type f -name '*xyzzy*' -exec rename -n 's/xyzzy_file_//' {} \;
However, there's probably a more efficient way.

Last edited by Turbocapitalist; 10-13-2019 at 10:05 AM.
 
1 members found this post helpful.
Old 10-13-2019, 10:21 AM   #5
dezix
Member
 
Registered: Sep 2017
Location: Frog's Land
Distribution: Debian
Posts: 98

Original Poster
Rep: Reputation: Disabled
@Turbocapitalist

I've tried to simplify the question but it wasn't the best way to ask, sorry.

I have some thing like :
Code:
.../parent/
	├── page.txt
	└── page/
		├── pasted_image000.png
		├── pasted_image001.png
		├── pasted_image002.png
		├── sous-page.txt
		└── sous-page/
			├── ...
			└── ...
that I want to turn in :

Code:
├── page.txt
└── page/
	├── page000.png
	├── page001.png
	├── page002.png
You're right, the final command I think about deals with rename
 
Old 10-13-2019, 10:43 AM   #6
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,137
Blog Entries: 6

Rep: Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826Reputation: 1826
Example with a sleep so that you can see whats happening.

Code:
cd /path/path

num=0
for i in *; do
    echo ""$i" > page"$((num++))".png"
    sleep .5
done
Use mv, rename with it.
 
Old 10-13-2019, 11:04 AM   #7
dezix
Member
 
Registered: Sep 2017
Location: Frog's Land
Distribution: Debian
Posts: 98

Original Poster
Rep: Reputation: Disabled
Code:
find . -name "pasted_image[0-9]*.png" -ok rename -n s/pasted_image/$(basename $(dirname '{}'))/ '{}' \;
< rename ... ./pasted_image000.png >*? y
rename(./pasted_image000.png, ./.000.png)
This works but putting dots not the parent folder name



Quote:
Originally Posted by teckk View Post
Example with a sleep so that you can see whats happening.

Code:
cd /path/path

num=0
for i in *; do
    echo ""$i" > page"$((num++))".png"
    sleep .5
done
Use mv, rename with it.
Thanks, but I don't understand how it works

I will try ...
 
Old 10-13-2019, 06:22 PM   #8
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
If you set the globstar shell option in bash then the construction 'for d in **/' returns all subdirectory names. These can be processed using parameter expansions, as shown in this one liner. It is wrapped in single parantheses so the command is run in a subshell.
Code:
 (shopt -s globstar; for d in **/; do e=${d%*/}; for f in $d*; do echo "$f is in directory $d with parent ${e##*/}"; done; done)
 
1 members found this post helpful.
Old 10-14-2019, 05:47 AM   #9
dezix
Member
 
Registered: Sep 2017
Location: Frog's Land
Distribution: Debian
Posts: 98

Original Poster
Rep: Reputation: Disabled
@allend
Great! Thank you for teaching something really new to me.

Finally, I think,
I will try to use smallpond's solution that looks the simplest way,
in some "for" loop

Thanks to every one for helping and driving me on the right track.
 
  


Reply

Tags
bash, folders, names, retrieving, scripting



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
Create 5 processes from a common parent and ensure that the parent terminates after c pro_learner Linux - Newbie 1 03-06-2013 09:25 AM
bash: recursive find, create parent folder, moving file moocan Programming 8 12-19-2010 12:16 AM
File descriptors shared between child and parent causing parent to hang. bharadiaam Linux - Newbie 1 03-02-2009 01:01 AM
automatically inherit parent folder permissions when copying files teixeira Linux - Newbie 3 07-08-2008 12:21 PM

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

All times are GMT -5. The time now is 07:51 PM.

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