bash, показать полный путь, исключить имя файла - PullRequest
0 голосов
/ 09 марта 2020

Я пытаюсь сделать рекурсивную печать всего пути к файлам, но исключаю файлы, насколько я знаю, использование find /this/path/ -type f напечатает

/this/path/file1
/this/path/file2
/this/path/also/file3
/this/path/also/this/file4

добавление | awk 'BEGIN { FS = "/"} ; { print $NF}' приведет к печати

file1
file2
file3
file4

Мне нужен какой-то способ напечатать полный путь каждого файла, но без реального файла, что-то вроде этого:

/this/path/
/this/path/
/this/path/also/
/this/path/also/this/

Ответы [ 2 ]

3 голосов
/ 09 марта 2020

Должны быть доступны команды basename и dirname:

find -type f -exec dirname {} \;

или

find -type f -print0 | xargs -0 dirname
2 голосов
/ 09 марта 2020

Если ваша версия find поддерживает -printf, очень легко получить имя dirname:

$ find /this/path/ -type f -printf '%h\n'
/this/path
/this/path
/this/path/also
/this/path/also/this

Также вы можете упростить получение базового имени с помощью той же техники:

$ find /this/path/ -type f -printf '%f\n'
file1
file2
file3
file4

Подробности от man find:

%f
File's name with any leading directories removed (only the last element). 
...
%h
Leading directories of file's name (all but the last element). If the file name contains
no slashes (since it is in the current directory) the %h specifier expands to ".".
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...