How do I retrieve all files in linux for given directory level. I want to absolute path aswell as the filename - linux

I'm trying to retrieve all files for a given directory level. The files retrieved should also show absolute path.
git ls-files | cut -f1 | uniq | grep '.xml'
I expect the results to be a list of files in the directory level/depth 1.

Using find: using -maxdepth flag set to 1, -type f to list only files, -iname to list file of matching type. -exec to perform some action on the matched files. readlink -f to print the full path of the files.
find . -maxdepth 1 -type f -iname "*.xml" -exec readlink -f {} \;

Related

How to make a FOR loop to move files to directory based on file extension

Im trying to sort thousands of files and put them in their own folder based on file extension. For example, JPG files to go into a JPG folder.
How can I create a for loop to address this?
What I have attempted to far:
# This listed out all the file extensions and the count for each extension:
find . -type f | rev | cut -d. -f1 | rev | tr '[:upper:]' '[:lower:]' | sort | uniq --count | sort -rn
#This was able to find all jpegs in the media folder
find /media -iname '*.jpg'
# This worked, however the MV command does not create the folder
find /media -iname '*.jpg' -exec mv '{}' /media/genesis/Passport/consolidated/jpg/ \; #
Im guessing that the for loop would be something like but I cant seem to figure it out:
for dir in 'find /media -iname "*.jpg"'; do
mkdir $dir;
mv $dir/*;
done
To find all the files of a particular extension and move then to a destination we can use find command as follows:
find . -name "*.jpg" -exec mv {} $destination/ \;
This being the critical logic, you can just create a for loop on top of this to iterate through all the known file extensions in your media folder
## List all the known extensions in your media folder.
declare -a arr=("jpg" "png" "svg")
## Refer question 1842254 to get all the extentsions in your folder.
## find . -type f | perl -ne 'print $1 if m/\.([^.\/]+)$/' | sort -u
## now loop through the above array
for i in "${arr[#]}"
do
echo "$i"
## Create a directory based on file extension
destination="$i"
mkdir $destination
find /media -iname "*.$i" -exec mv {} $destination/ \;
done
Note that you can change your destination to be whatever you want. I just left it in the same directory.
Here is an executable shell script for your reference: https://onlinegdb.com/NEQSpojhM

Copy recursive files of all the subdirectories

I want to copy all the log files from a directory which does not contain log files, but it contains other subdirectories with log files. These subdirectories also contain other subdirectories, so I need something recursive.
I tried
cp -R *.log /destination
But it doesn't work because the first directory does not contains log files. The response can be also a loop in bash.
find /path/to/logdir -type f -name "*.log" |xargs -I {} cp {} /path/to/destinationdir
Explanation:
find searches recursively
-type f tells you to search for files
-name specifies the name pattern
xargs executes commands
-I {} indicates an argument substitution symbol
Another version without xargs:
find /path/to/logdir -type f -name '* .log' -exec cp '{}' /path/to/destinationdir \;

Bash: Find files containing a certain string and copy them into a folder

What I want:
In a bash script: Find all files in current directory that contain a certain string "teststring" and cop them into a subfolder "./testfolder"
Found this to find the filenames which im looking for
find . -type f -print0 | xargs -0 grep -l "teststring"
..and this to copy found files to another folder (here selecting by strings in filename):
find . -type f -iname "stringinfilename" -exec cp {} ./testfolder/ \;
Whats the best way to combine both commands to achieve what I described at the top?
Just let find do both:
find . -name subdir -prune -o -type f -exec \
grep -q teststring "{}" \; -exec cp "{}" subdir \;
Note that things like this are much easier if you don't try to add to the directory you're working in. In other words, write to a sibling dir instead of writing to a subdirectory. If you want to wind up with the data in a subdir, mv it when you're done. That way, you don't have to worry about the prune (ie, you don't have to worry about find descending into the subdir and attempting to duplicate the work).

Linux find all files in sub directories and move them

I have a Linux-System where some users put files with ftp in a Directory. In this Directory there are sub-directories which the users can create. Now I need a script that searches for all files in those subdirectories and moves them in a single Directory (for backup). The Problem: The Sub directories shouldn´t be removed.
the directory for the users is /files/media/documents/
and the files have to be moved in the Directory /files/dump/. I don´t care about files in /files/media/documents/, they are already handled by another script.
I already tried this script:
for dir in /files/media/documents/
do
find "$dir/" -iname '*' -print0 | xargs -0 mv -t /files/dump/
done
Instead of iterating, you could just use find. In man-page there is a "-type" option documented, so for moving only files you could do:
find "/files/media/documents/" -type f -print0 | xargs -0 mv -t /files/dump/
You also won't like to find files in /files/media/documents/, but all sub-directories? Simply add "-mindepth":
find "/files/media/documents/" -type f -mindepth 1 -print0 | xargs -0 mv -t /files/dump/
Alternatively you could also use "-exec" to skip a second command (xargs):
find "/files/media/documents/" -type f -mindepth 1 -exec mv {} /files/dump/ \;

List all files in a directory with abosolute paths (excluding directories)

I've currently got:
ls -1 $(pwd)/*
Gives me all the files in a directory with absolute paths - but formats it with the directory at the start of each list of files.
Is there a way just to get a list of files in a directory recursively (absolute paths) - excluding the directory/sub-directories themselves?
find $(pwd) -type f -print
or
find $(pwd) -type f -ls
If you are feeding it into something else, you might want -print0 (to handle filenames with spaces).
E.g.: find . -type f -print0 | xargs --null --no-run-if-empty grep

Resources