ls command: how can I get a recursive full-path li

2019-01-09 20:35发布

How can I get ls to spit out a flat list of recursive one-per-line paths?

For example, I just want a flat listing of files with their full paths:

/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow
/home/dreftymac/stackoverflow/alpha.txt
/home/dreftymac/stackoverflow/bravo.txt
/home/dreftymac/stackoverflow/charlie.txt

ls -a1 almost does what I need, but I do not want path fragments, I want full paths.

23条回答
姐就是有狂的资本
2楼-- · 2019-01-09 21:19

Adding a wildcard to the end of an ls directory forces full paths. Right now you have this:

$ ls /home/dreftymac/
foo.txt
bar.txt
stackoverflow
stackoverflow/alpha.txt
stackoverflow/bravo.txt
stackoverflow/charlie.txt

You could do this instead:

$ ls /home/dreftymac/*
/home/dreftymac/.
/home/dreftymac/foo.txt
/home/dreftymac/bar.txt
/home/dreftymac/stackoverflow:
alpha.txt
bravo.txt
charlie.txt

Unfortunately this does not print the full path for directories recursed into, so it may not be the full solution you're looking for.

查看更多
小情绪 Triste *
3楼-- · 2019-01-09 21:20

If the directory is passed as a relative path and you will need to convert it to an absolute path before calling find. In the following example, the directory is passed as the first parameter to the script:

#!/bin/bash

# get absolute path
directory=`cd $1; pwd`
# print out list of files and directories
find $directory
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-09 21:21

I think for a flat list the best way is:

find -D tree /fullpath/to-dir/ 

(or in order to save it in a txt file)

find -D tree /fullpath/to-dir/ > file.txt
查看更多
beautiful°
5楼-- · 2019-01-09 21:21

Run a bash command with the following format:

find /path -type f -exec ls -l \{\} \;
查看更多
迷人小祖宗
6楼-- · 2019-01-09 21:22

Try the following simpler way:

find "$PWD"
查看更多
Summer. ? 凉城
7楼-- · 2019-01-09 21:23

Oh really a long list of answers. It helped a lot and finally I created by own which I was looking for :

To List All the Files in a directory and its sub-directories:

find "$PWD" -type f

To List All the Directories in a directory and its sub-directories:

find "$PWD" -type d

To List All the Directories and Files in a directory and its sub-directories:

find "$PWD"
查看更多
登录 后发表回答