Get a list of files with full path - unix

I would like to get a list of all the files in a directory hierarchy (like I would with ls -R), but such that instead of listing the name of the directory and its files beneath it, it would just output a list of files with their full path. Is this possible?

Use find for this type of thing.
find /home/me/subdir
will list all the files and directories, with full path, that live in /home/me/subdir.
find /home/me/subdir -type f
will only list files. (-type d for directories.)
If you need to match a filename glob, do like this:
find /home/me/subdir -type f -name "abc*"
Or exclude a file name pattern:
find /home/me/subdir -type f ! -name ".*"

Related

Linux- command line - How to grep smt of a hidden file inside a directory of all directories

I am in a directory that has let's say 100 directories (and nothing else) and each of them has another 50 directories (and nothing else) and each of the directory(of the 50) has some hidden files. All the 50 dirs have the same name for the hidden file.
How can I grep something in the hidden file?
Example:
grep "Killed" .log
(the .log file is inside each of the 50 dirs; but I am in the root of the 100 dirs)
Using GNU grep:
grep -r --include=.log 'Killed'
This starts a recursive grep in your current directory including only files matching the name .log.
The question is a bit ambiguous. Do you have multiple "hidden" files, and you only want to search for a string in files with a particular name, or do you want to search for the string in all of the files? Either way, it's pretty trivial:
find /root/dir -type f -exec grep pattern {} \; # Search all files
find /root/dir -type f -name '*.log' -exec grep pattern {} \; # Search only in files with names matching '*.log'
You'll often want to add a -H (or specify /dev/null as a second argument) to the invocation of grep to see filenames.

How to rename file from the specific pattern within the file

I would like to change the file name according to the specific pattern within the file. Let's say I have the unique pattern that starts with "XmacTmas". I would like to use this pattern to rename the file with the additional character like "_dbp1".
Now my file name is "xxo1" and I want "XmacTmas_dbp1".
How can I do this in for thousands of files with some script.
Thanks
find . -name 'XmacTmas*' -exec echo mv {} {}_dbp1 \;
find the files of interest and execute command after replacing {} with the found filename.
Escape the ;. Without the \, find would take it as part of the command to execute.
If only files in the actual directory are needed, add -maxdepth 0 before -name (or any other of find's numerous options)
If the result is as needed, remove the echo

List only folders which matches the wildcard expression

Is there a command which lists all folders that matches a wildcard expression? Example, if there are thousands of directories and I only want those ending in M or those starting in JO to be listed, can I do that with a certain Linux command? Thanks!
Use find command, for example:
# find anything that start with 'jo' end with 'm' (case insensitive)
find . -iname 'jo*m'
You can execute any command after that, for example:
# find just like above but case sensitive, and move them to `/tmp`
find . -name 'JO*M' -exec mv -v {} /tmp \;
To find only a directory, you can use -type d flag, for example:
# find any directory that start with JO
find . -name 'JO*' -type d
Explanation, first argument is the starting directory, . means current directory. The next argument means the search criteria -name for case sensitive search, -iname for case insensitive search, -type for type of item search, -exec to execute certain command where the {} is the file name matched. You can learn more here or for your specific case here.

Find a file in directory where i know part of the filename and the extension

I know the file im looking for begins with a data for example 20131111 and I know the file ends in .log, but I don't know the full file name,
what is a unix command that would allow me to see all files beginning with or containing this date and ending with .log.
Like this, for example:
find /certain/path -type f -name "20131111*.log"
-type f - just files.
-name "20131111*.log" files whose name starts with 20131111 and ends with log.

Use of find in unix on strange file/directory names [duplicate]

Im a beginner scripter, writing scripts in tcsh and csh(these are teached in my course)
Im writing a script which uses find for putting path of directories
this is the part of the script:
set list = (`find $PATH -type d`)
it works fine until the file or directory names arent named such as:
#fi##lename&& or −filename or :−,?!drectoryanem!-``
These special characters i couldnt handle i changed the find script to:
set list = ("`find $PATH -type d`")
bit none of these works, when i want to use the path from the list in this next script:
foreach i ($list:q)
foreach file (`find "$i" -maxdepth 1 -type f`)
....
end
end
it couldnt handle these special file names, so i get many errors like find: −."!filename: no such filename or directory
I worked it out
It had to be this way:
set subor = ("`find "'"$i"'" -type f -maxdepth 1`")
now it ignores everything in the filenames
and in:
foreach j ($subor:q)
i quoted it this way it ignores the white characters in file names

Resources