List files recursively in Linux CLI with path relative to the current directory

LinuxUnixRecursionLs

Linux Problem Overview


This is similar to this question, but I want to include the path relative to the current directory in unix. If I do the following:

ls -LR | grep .txt

It doesn't include the full paths. For example, I have the following directory structure:

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

The code above will return:

file.txt
file1.txt
file2.txt

How can I get it to include the paths relative to the current directory using standard Unix commands?

Linux Solutions


Solution 1 - Linux

Use find:

find . -name \*.txt -print

On systems that use GNU find, like most GNU/Linux distributions, you can leave out the -print.

Solution 2 - Linux

Use tree, with -f (full path) and -i (no indentation lines):

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

You can then use grep to filter out the ones you want.


If the command is not found, you can install it:

Type following command to install tree command on RHEL/CentOS and Fedora linux:

# yum install tree -y

If you are using Debian/Ubuntu, Mint Linux type following command in your terminal:

$ sudo apt-get install tree -y

Solution 3 - Linux

Try find. You can look it up exactly in the man page, but it's sorta like this:

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

so for your example

find . -name "*.txt"

should give you what you want.

Solution 4 - Linux

You could use find instead:

find . -name '*.txt'

Solution 5 - Linux

To get the actual full path file names of the desired files using the find command, use it with the pwd command:

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

Solution 6 - Linux

That does the trick:

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

But it does that by "sinning" with the parsing of ls, though, which is considered bad form by the GNU and Ghostscript communities.

Solution 7 - Linux

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

'sed' will erase 'your_path' from all 'find' results. And you recieve relative to 'DIR' path.

Solution 8 - Linux

Here is a Perl script:

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();

usage:

ls -LR | perl format_ls-LR.pl

Solution 9 - Linux

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.

Solution 10 - Linux

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

find / -name "filename" 

Solution 11 - Linux

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

Solution 12 - Linux

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.

Solution 13 - Linux

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)

Solution 14 - Linux

In mycase, with tree command

Relative path

tree -ifF ./dir | grep -v '^./dir$' | grep -v '.*/$' | grep '\./.*' | while read file; do
  echo $file
done

Absolute path

tree -ifF ./dir | grep -v '^./dir$' | grep -v '.*/$' | grep '\./.*' | while read file; do
  echo $file | sed -e "s|^.|$PWD|g"
done

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDarryl HeinView Question on Stackoverflow
Solution 1 - LinuxAndru LuvisiView Answer on Stackoverflow
Solution 2 - LinuxStephen IronsView Answer on Stackoverflow
Solution 3 - LinuxJonathan AdelsonView Answer on Stackoverflow
Solution 4 - LinuxSherm PendleyView Answer on Stackoverflow
Solution 5 - LinuxZaSterView Answer on Stackoverflow
Solution 6 - LinuxEl GuestoView Answer on Stackoverflow
Solution 7 - Linuxh-dimaView Answer on Stackoverflow
Solution 8 - LinuxEric KellerView Answer on Stackoverflow
Solution 9 - LinuxrxwView Answer on Stackoverflow
Solution 10 - Linuxuser2101432View Answer on Stackoverflow
Solution 11 - LinuxrajeshkView Answer on Stackoverflow
Solution 12 - Linuxrien333View Answer on Stackoverflow
Solution 13 - LinuxSireesh YarlagaddaView Answer on Stackoverflow
Solution 14 - LinuxkazuwombatView Answer on Stackoverflow