Linux Help
guides forums blogs
Home Desktops Distributions ISO Images Logos Newbies Reviews Software Support & Resources Linuxhelp Wiki

Welcome Guest ( Log In | Register )

Advanced DNS Management
New ZoneEdit. New Managment.


Sign Up Now
> how do I grep a portion of the current directory?
post Mar 24 2014, 08:45 AM
Post #1

Whats this Lie-nix Thing?

Group: Members
Posts: 2
Joined: 24-March 14
Member No.: 18,683

I need to copy a portion of the current directory into a variable for use within a bash script.

typing pwd returns "/apps/dir1/subdir1/subdir2/subdir3/subdir4"

how do I use grep (assuming grep is the right tool?) to select only the 1st 2 directories of the path. i.e. how do I get it to return "/apps/dir1"

I've tried:
pwd | grep /w+\/{2}

and probably 100 other variations of the commands (incl. xargs), but they only ever return an error, or nothing whatsoever.

Same question if I wanted only the 1st directory of pwd: "/apps"
or up to the 3rd directory: "/apps/dir1/subdir1"
or ONLY the 3rd directory: "/subdir1"

Please help a guy who knows this should be easier than he's making it?

Thank you!
Go to the top of the page
+Quote Post

Posts in this topic

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:


RSS Lo-Fi Version Time is now: 21st July 2018 - 08:44 AM