Deleting a specific sub directory under all directories - linux

I have several tests in a directory which were compiled in C and created the image or output in 'unmanaged' folders under each test directory. I would like to delete only the unmanaged folders under every test directory (a cleanup you can say) and keep all other directories like src, cmm, scf (scatter files) etc. unchanged.
I can do this either in Windows or Linux since the directory is mapped to Windows. Please let me know how do I proceed.

find . -name unmanaged -type d -exec rm -rf {} \;

Related

Copy files within multiple directories to one directory

We have an Ubuntu Server that is only accessed via terminal, and users transfer files to directories within 1 parent directory (i.e. /storage/DiskA/userA/doc1.doc /storage/DiskA/userB/doc1.doc). I need to copy all the specific files within the user folders to another dir, and I'm trying to specifically target the .doc extension.
I've tried running the following:
cp -R /storage/diskA/*.doc /storage/diskB/monthly_report/
However, it keeps telling me there is no such file/dir.
I want to be able to just pull the .doc files from all the user dirs and transfer to that dir, /storage/monthly_report/.
I know this is an easy task, but apparently, I'm just daft enough to not be able to figure this out. Any assistance would be wonderful.
EDIT: I updated the original to show that I have 2 Disks. Moving from Disk A to Disk B.
I would go for find -exec for such a task, something like:
find /storage/DiskA -name "*.doc" -exec cp {} /storage/DiskB/monthly_report/ \;
That should do the trick.
Use
rsync -zarv --include="*/" --include="*.doc" --exclude="*" /storage/diskA/*.doc /storage/diskB/monthly_report/

How to remove empty folders and subfolders using cronjob

I found a script online that works when entered into custom cronjobs on cpanel.
When i run this script, the code deletes the folder and everything within it.
rm -rf public_html/storage_area/images/
I would like to delete empty sub-folders housed within the images folder and not the actual images folder itself.
I do not have much technical knowledge so any help would be much appreciated. I have tried a few php scripts that i found online but did not have much luck so if there is something that exists even better.
Thank you for any assistance.
You just need to modify the command a bit.
If you need to remove only files inside that folder you can use,
rm -rf public_html/storage_area/images/*.*
The *.* will only remove files within the folder public_html/storage_area/images/ having an extension.
If you need to remove files and sub folders, then you need to use
rm -rf public_html/storage_area/images/*
If you only need to remove sub folders which are empty, you can use
find -type d -empty -delete
Before running the above command, you may need to verify whether the command is only returning empty folders. For that you can use,
find public_html/storage_area/images/ -type d -empty -print

Copying files from multiple directories to another directory using linux command line

I have a bunch of files in separate folders, and all of the folders are in one directory.
/var/www/folder1/file1.txt
/var/www/folder1/file2.txt
/var/www/folder1/file3.txt
/var/www/folder2/file4.jpg
/var/www/folder2/file5.jpg
/var/www/folder2/file6.jpg
/var/www/folder3/file7.pdf
/var/www/folder3/file8.doc
/var/www/folder3/file9.gif
I need everything inside of the folders that are inside of /var/www/ to be copied to another directory (say, /var/my-directory/), but not the actual folders. Based on the example above, I need /var/my-directory/` to look as follows:
/var/my-directory/file1.txt
/var/my-directory/file2.txt
/var/my-directory/file3.txt
/var/my-directory/file4.jpg
/var/my-directory/file5.jpg
/var/my-directory/file6.jpg
/var/my-directory/file7.pdf
/var/my-directory/file8.doc
/var/my-directory/file9.gif
I can't seem to figure out the command to do this. I've tried the following:
sudo cp -R /var/www/./. /var/my-directory/
But, that still copies all of the folders.
Is there any way to do what I'm trying to do?
Use find.
find /var/www/ -type f -exec cp '{}' /var/my-directory/ \;
The trick is -type f that only selects file.

how to delete all files in directory except one folder and one file?

I have application which has one folder called vendor and one file called .env. When ever i automatically publish my source code files to folder, all old files should get deleted except these two.
How can i do this in linux by using shell?
PS : I am trying to implement rollback mechanism in Jenkins. I will copy artifacts from old build and transfer them to server using ssh. But this will be a copy operation. So I want to delete previous files before starting copy using SSH.
You can use find:
find ! \( -name 'name1' -o -name 'name2' \) -exec rm -r {} +
try with this command
rm !(<filename>)

Linux Shell: remove all HTML files in tree while leaving directory structure

I'm trying to remove all .html files from the directory generated and from all subfolders there but it needs to leave all other files and directories alone.
I tried going through folder by folder and running rm *.html but this takes a long time as there are 20+ subfolders which also have subfolders. I tried looking the man pages for rm but nothing obvious jumped out. I'm sure there's a way to do this in one shot but I don't know how. Any ideas?
I think this may work:
cd generated
find . -type f -iname "*.html" -delete

Resources