Linux list all paths

How do I list files with full paths in Linux?

In Linux, is there an equivalent to Dir /s /a /b where the full path and filename is listed? I’m new to Linux, and without a GUI, I want to get an idea of the structure of what’s on the hard disk.

6 Answers 6

Use the find command. By default it will recursively list every file and folder descending from your current directory, with the full (relative) path.

If you want the full path, use: find «$(pwd)» .

  • If you want to restrict it to files or folders only, use find -type f or find -type d , respectively.
  • If you want it to stop at a certain directory depth, use find -maxdepth 2 , for example.

Read Finding Files for an extensive manual on GNU find , which is the default on Linux.

Notes:

  • . : current folder
  • remove -maxdepth 1 to search recursively
  • -type f : find files, not directories ( d )
  • -not -path ‘*/\.*’ : do not return .hidden_files
  • sed ‘s/^\.\///g’ : remove the prepended ./ from the result list

For completeness, the ls -lR / command will list the name of each file, the file type, file mode bits, number of hard links, owner name, group name, size, and timestamp of every file (that you have permission to access) from the root directory down. ( l is for long list ie all that info, R is to recurse through directories, / starts at the root of the filesystem.)

There are a number of params to make the output info closer to dir /S /A , but I have to admit I can’t work out how to translate the /B .

For useful info, I would try: ls -lAFGR —si /

  • l = long list (as mentioned above)
  • A = almost all files (doesn’t include . and .. in each dir, but does show all hidden files)
  • F = show file indicator, (one of * for exe files, / for directories, @ for symbolic links, | for FIFOs, = for sockets, and > for doors)
  • G = don’t show group info (remove this if you want to see it)
  • R = recursively list directories (subdirectories) and
  • —si = show the file size in human readable eg 1M format (where 1M = 1000B)

ls can provide an easier to read synopsis of directories and files within those directories, as find ‘s output can be difficult to scan when files are contained within really long directory structures (spanning multiple lines). The corollary is that each file is listed on its own (ie without directory path information) and you may have to go back a couple of pages/screens to find the directories a particular file is located in.

Also, find doesn’t contain the /A information in the DIR command. I’ve suggested a number of attributes in the command I’ve shown (that coincidentally show the extra usefulness that you get from linux over a certain proprietary system), but if you read the man and info pages on ls , you will be able to see what to include or not.

Источник

Linux / UNIX List Just Directories Or Directory Names

You can use combination of ls command, find command, and grep command to list directory names only. You can use the find command too. In this quick tutorial you will learn how to list only directories in Linux or UNIX.

Читайте также:  Canon ir 1510 драйвер windows 10
Tutorial details
Difficulty level Easy
Root privileges No
Requirements Linux, macOS, or Unix terminal
Est. reading time 5 minutes

Display or list all directories in Unix

Type the following command:
$ ls -l | grep `^d’
$ ls -l | egrep `^d’
Or better try the following ls command only to list directories for the current directory:
$ ls -d */
Sample outputs:

Fig.01: List Directories in Unix and Linux Systems

Linux list only directories using ls command

Run the following ls command:
ls -d */

Listing only directories using ls command in Linux or Unix-like systems

Linux Display or list only files

Type the following command to display list only files in Linux or Unix:
$ ls -l | egrep -v ‘^d’
$ ls -l | egrep -v ‘^d’
The grep command is used to searches input. It will filter out directories name by matching first character ‘ d ‘. To reverse effect i.e. just to display files you need to pass the -v option. It invert the sense of matching, to select non-matching lines.

Task: Create bash shell aliases to save time

You can create two aliases as follows to list only directories and files.
alias lf=»ls -l | egrep -v ‘^d'»
alias ldir=’ls -d */’
##alias ldir=»ls -l | egrep ‘^d'»
Put above two aliases in your bash shell startup file:
$ cd
$ vi .bash_profile

Append two lines:
alias lf=»ls -l | egrep -v ‘^d'»
alias ldir=’ls -d */’
#alias ldir=»ls -l | egrep ‘^d'»
Save and close the file in vim. Now just type lf – to list files. Again run ldir to list directories only:
$ cd /etc
$ ldir
Sample outputs:

List directory names only:
$ cd /etc
$ ldir

Sample outputs:

Use find command to list either files or directories on Linux

The find command can be used as follows to list all directories in /nas, enter:

Pass the -maxdepth 0 to limit listing to the starting-points i.e. the current working directory only:
find /path/to/dir -maxdepth 1 -type d
find . -maxdepth 1 -type d
find . -maxdepth 1 -type d -ls

Listing only directories using the find command in Linux

Putting it all together

Say you want to find all directories ending with .bak extension and delete it, run the following find command in the current directory:
find . -type d -iname «.bak» -delete
Verify it:
find . -type d -iname «.bak» -ls
The following shell script does two things for Apache/Nginx/Lighttpd Webroot such as /webroot/:

  1. First, finds all files and directories and set permission to read-only for security reasons.
  2. Second, it allows our web server to read files regardless of permission so that we don’t get an HTTP/403 error.

In other words, all write permissions are removed from Webroot. The server/web-app can only read files but can not alter any files or upload any files. It helps reduces attack surfaces provided that you configure the rest of the server and web application firewall correctly.

Источник

How to list all files in a directory with absolute paths

I need a file (preferably a .list file) which contains the absolute path of every file in a directory.

Example dir1: file1.txt file2.txt file3.txt

How can I accomplish this in linux/mac?

10 Answers 10

You can use find. Assuming that you want only regular files, you can do:

You can adjust the type parameter as appropriate if you want other types of files.

It’s the shell that computes the list of (non-hidden) files in the directory and passes the list to ls . ls just prints that list here, so you could as well do:

Note that it doesn’t include hidden files, includes files of any type (including directories) and if there’s no non-hidden file in the directory, in POSIX/csh/rc shells, you’d get /current/wd/* as output. Also, since the newline character is as valid as any in a file path, if you separate the file paths with newline characters, you won’t be able to use that resulting file to get back to the list of file reliably.

With the zsh shell, you could do instead:

  • -rC1 prints r aw on 1 C olumn.
  • -N , output records are NUL-delimited instead of newline-delimited (lines) as NUL is the only character that can’t be found in a file name.
  • N : expands to nothing if there’s no matching file ( nullglob )
  • D : include hidden files ( dotglob ).
  • -. : include only regular files ( . ) after symlink resolution ( — ).

Then, you’d be able to do something like:

To remove those files for instance.

You could also use the 😛 modifier in the glob qualifiers to get the equivalent of realpath() on the files expanded from the globs (gets a full path exempt of any symlink component):

To see just regular files —

Another way with tree , not mentioned here, it goes recursively and unlike find or ls you don’t have any errors (like: Permission denied , Not a directory ) you also get the absolute path in case you want to feed the files to xargs or other command

the options meaning:

To install tree :

sudo apt install tree on Ubuntu/Debian

sudo yum install tree on CentOS/Fedora

sudo zypper install tree on OpenSUSE

You can just use realpath or readlink this naughty way:

When ls prints to a TTY it formats the file names in columns, but when it’s writing to a file, pipe, or other non-TTY it behaves like ls -1 and prints one file name per line. You can check this by running ls | cat in place of ls . [1]

  • xargs build and execute command lines from standard input.
  • realpath : return the canonicalized absolute pathname
  • readlink : read value of a symbolic link
  • Use realpath — to make it treat everything that follows as parameters instead of options, if files could have » -something «.
  • If some files have spaces you could:

In a past Linux environment, I had a resolve command that would standardize paths, including making a relative path into an absolute path. I can’t find it now, so maybe it was written by someone in that organization.

You can make your own script using functions in the Python or Perl standard libraries (and probably other languages too).

Then, you would solve your problem with:

With this command, you can also do things like this:

Recursive files can be listed by many ways in linus. Here i am sharing one liner script to clear all logs of files(only files) from /var/log/ directory and second check recently which logs file has made an entry.

note: for directory location you can also pass $PWD instead of /var/log.

To list the full path of all commands (apps/programs) accessible to the user. (revised to address most, but not all limitations outlined in the comments)

NOTE
The PATH variable would normally have a colon ( : ) either at the beginning or at the end, but not both. A colon at the beginning or end signifies to search the current directory as well. Standard practice is for it to be at the end so as to never override standard utility programs. The sed substitutions here handle either case.

Explanation.

  • eval
    Frankly, I don’t know why eval is needed here, but it is. Without it, I get.

ls: cannot access ‘./<.,>[[:word:]]‘: No such file or directory
ls: cannot access ‘/home/alpha/bin/<.,>[[:word:]]
‘: No such file or directory
ls: cannot access ‘/usr/local/sbin/<.,>[[:word:]]‘: No such file or directory
ls: cannot access ‘/usr/local/bin/<.,>[[:word:]]
‘: No such file or directory
ls: cannot access ‘/usr/sbin/<.,>[[:word:]]‘: No such file or directory
ls: cannot access ‘/usr/bin/<.,>[[:word:]]
‘: No such file or directory
ls: cannot access ‘/sbin/<.,>[[:word:]]‘: No such file or directory
ls: cannot access ‘/bin/<.,>[[:word:]]
‘: No such file ordirectory

Источник

How can I generate a list of files with their absolute path in Linux?

I am writing a shell script that takes file paths as input.

For this reason, I need to generate recursive file listings with full paths. For example, the file bar has the path:

but, as far as I can see, both ls and find only give relative path listings:

It seems like an obvious requirement, but I can’t see anything in the find or ls man pages.

How can I generate a list of files in the shell including their absolute paths?

26 Answers 26

If you give find an absolute path to start with, it will print absolute paths. For instance, to find all .htaccess files in the current directory:

or if your shell expands $PWD to the current directory:

find simply prepends the path it was given to a relative path to the file from that path.

Greg Hewgill also suggested using pwd -P if you want to resolve symlinks in your current directory.

gives the full absolute path. but if the file is a symlink, u’ll get the final resolved name.

Use this for dirs (the / after ** is needed in bash to limit it to directories):

this for files and directories directly under the current directory, whose names contain a . :

this for everything:

In bash, ** is recursive if you enable shopt -s globstar .

This looks only in the current directory. It quotes «$PWD» in case it contains spaces.

Command: ls -1 -d «$PWD/»*

This will give the absolute paths of the file like below.

The $PWD is a good option by Matthew above. If you want find to only print files then you can also add the -type f option to search only normal files. Other options are «d» for directories only etc. So in your case it would be (if i want to search only for files with .c ext):

or if you want all files:

Note: You can’t make an alias for the above command, because $PWD gets auto-completed to your home directory when the alias is being set by bash.

If you give the find command an absolute path, it will spit the results out with an absolute path. So, from the Ken directory if you were to type:

(instead of the relative path find . -name bar -print )

Therefore, if you want an ls -l and have it return the absolute path, you can just tell the find command to execute an ls -l on whatever it finds.

NOTE: There is a space between <> and ;

You’ll get something like this:

If you aren’t sure where the file is, you can always change the search location. As long as the search path starts with «/», you will get an absolute path in return. If you are searching a location (like /) where you are going to get a lot of permission denied errors, then I would recommend redirecting standard error so you can actually see the find results:

( 2> is the syntax for the Borne and Bash shells, but will not work with the C shell. It may work in other shells too, but I only know for sure that it works in Bourne and Bash).

Источник

Оцените статью