How can I list all the files in a directory and all its sub-directories? [closed] - linux

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
How can I list all the files in a directory and all its sub-directories?
tree can accomplish this job:
$ tree
.
├── dir
│   └── f1
└── f2
1 directory, 2 files
But I want files to be listed in this format:
dir/f1
f2

Pass options -i and -f to tree:
tree -if
Option -i disables the printing of the indentation lines, option -f prints a path prefix for each file. This will however still list non-leaf directories.

Use the find command.
find . -type f

Apart from above mentioned solution, you can list the file in directory and its subdirectory using "ls" command with "-R" option.
ls -lR

One way to get this listing is using printf and ls -r
printf "%s\n" "$(ls -r)"

Related

How to find files which contain specific string in directory using command "find"? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 6 years ago.
Improve this question
This is my directory structure.
dirOut
├── dirIn1
│   ├── temp1
│   └── temp2
├── dirIn2
└── dirIn3
├── temp1
├── temp2
├── temp3
└── temp4
dir is directory and temp is file.
I want to find files which contain specific string "Hello".
How do I use command "find" to find.
Use grep, not a find, when you find files base on the content.
grep -lr Hello .
-l : Normally grep print matching lines, with -l option, it just print the matched filenames.
-r : recursively find files under the directory.
find dirOut -type f -exec grep -l Hello {} +
The -l option tells grep to just list the filename if it finds a match, rather than showing all the matching lines.
You could also do it using the -R option to grep to search a directory recursively, rather than using find.
grep -R -l Hello dirOut
find . -type f -name \* -exec grep -l "hello" {} \;
Execute this command while in dirOut.

Move all files whose names contain a capital letter from source directory to target directory? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
How to write this linux command?
Move all files whose names contain a capital letter from source directory to target directory?
If I read your question correctly, use this:
mv src/*[A-Z]* target/
The obvious but wrong solution is
mv src/*[A-Z]* dest
However, the order of letters is locale dependent. [A-Z] therefore can contain lower case letters:
$> touch abc aBc
$> export LC_ALL=C
$> ls *[A-Z]*
abc
$> LC_ALL=en_US
$> ls *[A-Z]*
aBc abc
so make sure to set LC_ALL properly.
export LC_ALL=C
mv src/*[A-Z]* dest
BTW: *[A-Z]* is evaluated by the shell, not mv. Therefore the following does not work:
LC_ALL=C mv rc/*[A-Z]* dest ## does not work
This version ensure that only "Files" in root source folder are moved to target directory:
find /source/*[A-Z]* -maxdepth 1 -type f -exec mv {} /target \;

Recursively doing the command ls without -R [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I am trying to find a way to recreate the output of ls -R (linux) without using the option -R i.e without the recursion command, is this at all possible?
There are no other constraints.
shopt -s globstar nullglob
printf "%s\n" **
or
find .
The closest I can think of right now is to recurse through all given directories using find and to perform a listing on each. I used ls -1 because I noticed that ls -R defaults to a single column when redirected into a file; you may choose to omit the -1 option.
for dir in `find . -type d`; do
echo $dir:
ls -1 $dir
done
However, it doesn't work with filenames that contain spaces. I'm still looking for a way around that...

List of All Folders and Sub-folders [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
In Linux, I want to find out all Folder/Sub-folder name and redirect to text file
I tried ls -alR > list.txt, but it gives all files+folders
You can use find
find . -type d > output.txt
or tree
tree -d > output.txt
tree, If not installed on your system.
If you are using ubuntu
sudo apt-get install tree
If you are using mac os.
brew install tree
find . -type d > list.txt
Will list all directories and subdirectories under the current path. If you want to list all of the directories under a path other than the current one, change the . to that other path.
If you want to exclude certain directories, you can filter them out with a negative condition:
find . -type d ! -name "~snapshot" > list.txt
As well as find listed in other answers, better shells allow both recurvsive globs and filtering of glob matches, so in zsh for example...
ls -lad **/*(/)
...lists all directories while keeping all the "-l" details that you want, which you'd otherwise need to recreate using something like...
find . -type d -exec ls -ld {} \;
(not quite as easy as the other answers suggest)
The benefit of find is that it's more independent of the shell - more portable, even for system() calls from within a C/C++ program etc..

How to show a 'grep' result with the complete path or file name [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 10 months ago.
Improve this question
How can I get the complete file path when I use grep?
I use commands like
cat *.log | grep somethingtosearch
I need to show the result with the complete file path from where the matched result were taken out.
How can I do it?
Assuming you have two log-files in:
C:/temp/my.log
C:/temp/alsoMy.log
'cd' to C: and use:
grep -r somethingtosearch temp/*.log
It will give you a list like:
temp/my.log:somethingtosearch
temp/alsoMy.log:somethingtosearch1
temp/alsoMy.log:somethingtosearch2
I think the real solution is:
cat *.log | grep -H somethingtosearch
Command:
grep -rl --include="*.js" "searchString" ${PWD}
Returned output:
/root/test/bas.js
If you want to see the full paths, I would recommend to cd to the top directory (of your drive if using Windows)
cd C:\
grep -r somethingtosearch C:\Users\Ozzesh\temp
Or on Linux:
cd /
grep -r somethingtosearch ~/temp
If you really resist on your file name filtering (*.log) and you want recursive (files are not all in the same directory), combining find and grep is the most flexible way:
cd /
find ~/temp -iname '*.log' -type f -exec grep somethingtosearch '{}' \;
It is similar to BVB Media's answer.
grep -rnw 'blablabla' `pwd`
It works fine on my Ubuntu 16.04 (Xenial Xerus) Bash.
For me
grep -b "searchsomething" *.log
worked as I wanted
This works when searching files in all directories.
sudo ls -R | grep -i something_bla_bla
The output shows all files and directories which include "something_bla_bla". The directories with path, but not the files.
Then use locate on the wanted file.
The easiest way to print full paths is to replace the relative start path with the absolute path:
grep -r --include="*.sh" "pattern" ${PWD}
Use:
grep somethingtosearch *.log
and the filenames will be printed out along with the matches.

Resources