Programing

현재 디렉토리에 상대적인 경로를 사용하여 Linux CLI에서 재귀 적으로 파일 나열

lottogame 2020. 4. 28. 08:18
반응형

현재 디렉토리에 상대적인 경로를 사용하여 Linux CLI에서 재귀 적으로 파일 나열


이것은 이 질문 과 비슷 하지만 유닉스의 현재 디렉토리에 상대적인 경로를 포함하고 싶습니다. 내가 다음을 수행하면 :

ls -LR | grep .txt

전체 경로는 포함되지 않습니다. 예를 들어 다음 디렉토리 구조가 있습니다.

test1/file.txt
test2/file1.txt
test2/file2.txt

위의 코드는 다음을 반환합니다.

file.txt
file1.txt
file2.txt

표준 Unix 명령을 사용하여 현재 디렉토리에 상대적인 경로를 포함 시키려면 어떻게해야합니까?


찾기 사용 :

find . -name \*.txt -print

대부분의 GNU / Linux 배포판과 같이 GNU find를 사용하는 시스템에서는 -print를 생략 할 수 있습니다.


(전체 경로) 및 (들여 쓰기 라인 없음 )과 tree함께를 사용하십시오 .-f-i

tree -if --noreport .
tree -if --noreport directory/

그런 다음을 사용 grep하여 원하는 것을 필터링 할 수 있습니다 .


명령이 없으면 설치할 수 있습니다.

RHEL / CentOS 및 Fedora linux에 tree 명령을 설치하려면 다음 명령을 입력하십시오.

# yum install tree -y

Debian / Ubuntu를 사용하는 경우 터미널에서 Mint Linux 유형 다음 명령을 입력하십시오.

$ sudo apt-get install tree -y

시도하십시오 find. 맨 페이지에서 정확하게 찾을 수 있지만 다음과 같습니다.

find [start directory] -name [what to find]

예를 들어

find . -name "*.txt"

당신이 원하는 것을 주어야합니다.


대신 find를 사용할 수 있습니다.

find . -name '*.txt'

그것은 트릭을 수행합니다.

ls -R1 $PWD | while read l; do case $l in *:) d=${l%:};; "") d=;; *) echo "$d/$l";; esac; done | grep -i ".txt"

그러나 lsGNU와 Ghostscript 커뮤니티에서 나쁜 형식으로 간주되는 구문 분석을 통해 "죄닝" 함으로써 그렇게합니다.


DIR=your_path
find $DIR | sed 's:""$DIR""::'

'sed'는 모든 'find'결과에서 'your_path'를 지 웁니다. 그리고 당신은 'DIR'경로와 관련하여받습니다.


find 명령을 사용하여 원하는 파일의 실제 전체 경로 파일 이름을 얻으려면 pwd 명령과 함께 사용하십시오.

find $(pwd) -name \*.txt -print

다음은 Perl 스크립트입니다.

sub format_lines($)
{
    my $refonlines = shift;
    my @lines = @{$refonlines};
    my $tmppath = "-";

    foreach (@lines)
    {
        next if ($_ =~ /^\s+/);
        if ($_ =~ /(^\w+(\/\w*)*):/)
        {
            $tmppath = $1 if defined $1;    
            next;
        }
        print "$tmppath/$_";
    }
}

sub main()
{
        my @lines = ();

    while (<>) 
    {
        push (@lines, $_);
    }
    format_lines(\@lines);
}

main();

용법:

ls -LR | perl format_ls-LR.pl

You could create a shell function, e.g. in your .zshrc or .bashrc:

filepath() {
    echo $PWD/$1
}

filepath2() {
    for i in $@; do
        echo $PWD/$i
    done
}

The first one would work on single files only, obviously.


Find the file called "filename" on your filesystem starting the search from the root directory "/". The "filename"

find / -name "filename" 

If you want to preserve the details come with ls like file size etc in your output then this should work.

sed "s|<OLDPATH>|<NEWPATH>|g" input_file > output_file

In the fish shell, you can do this to list all pdfs recursively, including the ones in the current directory:

$ ls **pdf

Just remove 'pdf' if you want files of any type.


You can implement this functionality like this
Firstly, using the ls command pointed to the targeted directory. Later using find command filter the result from it. From your case, it sounds like - always the filename starts with a word file***.txt

ls /some/path/here | find . -name 'file*.txt'   (* represents some wild card search)

참고URL : https://stackoverflow.com/questions/245698/list-files-recursively-in-linux-cli-with-path-relative-to-the-current-directory

반응형