I grab the contents of a directory with du -a /test1 | cut -f2
and get this:
/test1/file1
/test1/directory1
/test1/directory1/file2
My hope is to use something like cut
, sed
, or grep
to say "whatever $(pwd)
is, chop that off the front of the output."
For instance, if /test1/ contained another directory named 'test1', I'd want to retain that sub-directory in my output, so if I had:
/test1/test1/
my output would ideally be
test1/
Thank you for your help.
EDIT:
I have tried the tips outlined in Use sed on a string variable rather than a file. I can get output similar to what my example looks for, but it either takes out more than one instance of /test1 per line or it only operates on the first line of the variable. It's not acting the same as "For the beginning of each line, remove $(pwd)
, but only once per-line"
The use sed on a string answer is only working on the very first line of my multi-line variable. The topic of multi-line variables are not addressed in the answer given. Their solution works because the PATH variable doesn't contain newlines. In my case, it only works on the first line of the variable which is not the desired output.
du
to use directories? One would normally use either shell globbing orfind
for that. Why don't you simply change the working directory to the "prefix" that you want to remove? – David Foerster Dec 30 '17 at 00:38