How to delete files under subdirectories but not deleting subdirectories themselves in linux - linux

I have the following directory structure:
/archive/file1.csv
/archive/file2.csv
/archive/myfile/my.txt
/archive/yourfile/your.txt
I want to delete all files under /archive but not its subfolders, so after deletion, the directory structure should look like:
/archive/
/archive/myfile/
/archive/yourfile/
I have tried the following two commands, but the files under the subfolders are not deleted (ie. my.txt and your.txt), anyone know why ?
find -L /archive ! -type d -exec rm -rfv {} +
find -L /archive -type f -exec rm -rfv {} +

use find
$ find . ! -type d -delete
make sure you're in the right path.

Related

Remove dirs from parent dir without deleting parent directory (Linux)

I am using the following command to remove all dirs older than 1 minute from the following path:
find /myhome/me/xyz/[0-9][0-9]/[0-9][0-9][0-9][0-9]/my_stats/ -mmin +1 -exec rm -rf {} \;
folder structure :
/home/myhome/me/xyz/<2 digit name>/<4 digit name>/my_stats/
There could be multiple dirs or a single dir inside my_stats. The issue is when there is a single folder inside my_stats, the find command is deleting the my_stats dir as well.
Is there a way to solve this?
Thanks
If I understand your question correctly you are probably looking for this:
find /myhome/me/xyz/[0-9][0-9]/[0-9][0-9][0-9][0-9]/my_stats/ -mmin +1 -maxdepth 1 -mindepth 1 -type d -exec rm -rf {} \;
The -mindepth 1 parameter is what excludes the my_stats directory from the listing, as it is located at depth 0.
The -maxdepth 1 parameter will not show subdirs of subdirs (you are deleting their parents recursively anyway).
The -type d parameter limits the output to directories only, not ordinary files.

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).

How to delete content of file present in multiple directories in linux

Directory A having two sub directories B and C. Both B and C having same text file like "abc.txt". From directory A itself how to delete content abc.txt in both directories
If there could be more than two subdirectories in A, but you
want to restrict you to B and C, you can use
rm A/{B,C}/abc.txt
to delete both files.
To empty their content, use
: > A/{B,C}/abc.txt
Delete the actual files:
find A/ -name "abc.txt" -delete
Delete the "content" of the files:
find A/ -name "abc.txt" -exec truncate -s 0 {} \;
The more general way is to use find:
find . -type f -name "file" -exec rm -f {} \;
The explanation of the command is:
-name "file" : file name.
-exec rm -f {} \; : delete the files that match.
-type f : specify the type of file, directory are excluded.
Use Kleene star :
rm A/*/abc.txt
find A/ -name "abc.txt" -type f -exec rm -rf{}\;

Find and files of same name only in directory below and copy to a different directory

This script is finding the correct file but it is also searching through other directories and copying them. How can I find only the specific files, rename to data.log_$1 and cp in new directory.
find . -mindepth 1 -maxdepth 2 -type f -name Data.log -exec cp -rfp {} / extracted/ \;
It seems you have a stray slash there: cp -rfp {} / extracted/ should probably be cp -rfp {} extracted/ (or maybe cp -rfp {} /extracted/)

Create file in Linux and replace content

I have a project in Linux. I want to create a file named index.html in all folders.
So I have used the following command:
find . -type d -exec touch {}/index.html \;
It's working! Now I'm trying to copy the existing file from a given location and it to be automatically replaced into all the folders of my project.
This should actually work exactly in the same way:
find . -type d -exec cp $sourcedir/index.html {}/index.html \;
If I understand your question correctly, what you want is to copy a given file in all the directories.
You can use a similar find command :
find . -type d -exec cp -f /tmp/index.html {} \;
where /tmp/index.html is path to the original file (replace it with your own path).
Also, you don't need to create the files if your final objective is to replace them with the original file.
tar -cvzf index.tar.gz `find . -type f -iname 'index.html'` && scp index.tar.gz USER#SERVER:/your/projec/root/on/SERVER && ssh USER#SERVER "tar -xvzf index.tar.gz"
Or if you're in the proper directory localhost, and rsync is available:
rsync -r --exclude='**' --include='**/index.html' . USER#SERVER:/your/projec/root/on/SERVER
HTH

Resources