Bash: How to get part of the directory name?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash: How to get part of the directory name?
# 1  
Old 08-12-2010
Bash: How to get part of the directory name?

How can I write a script that determines the directory the user is in, and displays that path up until a particular point?

Specifically, I need to find the text "packages" in the directory name, then I need to capture that directory and the one below it.

For example, if the user is in the /home/smith/packages/cat/abc/def directory, I want to display "/home/smith/packages/cat/".

If the user is in the /home/smith/packages/dog/hij/klm/nop qrs/tuv directory, I want to display "/home/smith/packages/dog/".
# 2  
Old 08-12-2010
Code:
$ P=/home/smith/packages/cat/abc/def
$ echo $P | sed "s@\(.*/packages/[^/]*/\).*@\1@"
/home/smith/packages/cat/

This User Gave Thanks to Scott For This Post:
# 3  
Old 08-12-2010
Thank you. It works. I have no freakin' idea how, but it works.

How can I get up to speed on this? Are those regular expressions?
# 4  
Old 08-12-2010
Hi.

Yes, it's a regular expression.

Neo wrote a blog entry on a cool tool.

Aside from that, googling "regular expression" should give many good results (and, admittedly many not so good results Smilie).
# 5  
Old 08-12-2010
What are the @ characters for? Most of the sed commands I have seen have the format:
Code:
sed "s/old/new/g" *

I can't find a reference to the @ character in the context of a regular expression or as sed syntax.
# 6  
Old 08-12-2010
Quote:
Originally Posted by RickS
What are the @ characters for? Most of the sed commands I have seen have the format:
Code:
sed "s/old/new/g" *

I can't find a reference to the @ character in the context of a regular expression or as sed syntax.
The @ is not a character part of the regex. It is the boundary symbol Scottn choose to build the substitution for the sed utility.
Usually sed uses `/' with the `s' as in sed 's/old/new/' but when you need to use the `/' as part of the regex, it can become a mess to understand, so to make it a little easier, sed allows you to use any other symbol and as long you are consistent it works.
Code:
sed 's:old:new:'
sed 's!old!new!'
sed 's@old@new@'

All of them do the same.

Last edited by Scott; 08-12-2010 at 05:22 PM.. Reason: Added code tags
This User Gave Thanks to Aia For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Linux cant cd into directory and part of group

I am part of the group group1. The directory permission I am trying to cd into are 770 for both the parent directory and child directory but I still can not cd into. What am I doing wrong? $ ls -l /NAS/infa/ drwxrwxr-x. 22 user1 group1 506 Jun 6 17:05 infa_shared $... (5 Replies)
Discussion started by: cokedude
5 Replies

2. Shell Programming and Scripting

Create directory and sub-directory with awk and bash

In the below I am trying to create a parent directory using the R_2019 line from f1 if what above it is not empty. I then create sub-directories under each parent if there is a match between $2 of f1 and $2. Inside each sub-folder the matching paths in $3 and $4 in f2are printed. If there is no... (2 Replies)
Discussion started by: cmccabe
2 Replies

3. UNIX for Advanced & Expert Users

A way to print only part of directory path

Hi, So I struggled to find a solution to the following problem: I want to make sed print only part of multiple different paths. So lets say we have /path/path1/path2/logs/bla/blabla /path/path1/path2/path3/logs/yadda/yadda/yadda Can someone suggest a way to make sed or other... (5 Replies)
Discussion started by: dampio
5 Replies

4. Shell Programming and Scripting

Add part of directory name to filename

Hello, I need to add a part of folder name to the files inside it. For instance the file is HMCBackup_20150430.155027.tgz and it is under directory /nim/dr/HMCBackup/cops22 I need to add cops22 to the file name so as it would be cops22_HMCBackup_20150430.155027.tgz Any help in doing... (10 Replies)
Discussion started by: hasn318
10 Replies

5. Shell Programming and Scripting

Bash to calculate average of all files in directory and output by part of filename

I am trying to use awk to calculate the average of all lines in $2 for every file in a directory. The below bash seems to do that, but I cannot figure out how to capture the string before the _ as the output file name and have it be tab-delimeted. Thank you :). Filenames in... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

Bash executes first part of script but not second

My bash below verifies the integrity of all .bam in a directory and writes the out output to a .txt file. That is part one of the script that works. The second part of the bash searches each one of the .txt files for a string "(SUCCESS)" and if found display a message and if it is not found... (6 Replies)
Discussion started by: cmccabe
6 Replies

7. Shell Programming and Scripting

Selecting/using part of directory names in script

I'm making a shell script to: -copy directories to a new location -perform conversions on the files within the copied directories -move the newly created files to a new directory Please see my super basic script and notes below... and thank you thank you thank you in advance !! ... (1 Reply)
Discussion started by: kayzee
1 Replies

8. Shell Programming and Scripting

getting part of directory after a certain /

i have a directory like this: i want to get the subpaths: /home/user/public_html/a/b subpath= a/b /home/user/public_html/a/b/c subpath= a/b/c i can get the user like echo $fulldirectory |awk -F/ '{print $3}' but im not sure how to get the rest of it after public_html any ideas?... (3 Replies)
Discussion started by: vanessafan99
3 Replies

9. Shell Programming and Scripting

return part of a string in bash

Hi, I would like to return the last part of a string in an array of strings in bash. The array contains in each position the content below: and I would like to return only the last part of each string. The correct result would be: How can I do that in bash using AWK?... (7 Replies)
Discussion started by: anishkumarv
7 Replies

10. Shell Programming and Scripting

current directory as part of the csh prompt

I would like my csh prompt to behave like the linux csh prompt setting done by linux command (set prompt="%n@%m %c]$ ") how do I do that? What I'm trying to do is that I would like to see what directory I'm in by looking at the prompt. I've figured out that %n is like $user, and %m is like... (3 Replies)
Discussion started by: jamesloh
3 Replies
Login or Register to Ask a Question