How to remove all directories in a folder in command line? - linux

I want to remove all directories in a folder, no mater the specified directory is empty or not.
Can I do this in command line?

If I understand you correctly, you want to remove all subdirectories in a folder without deleting the files.
Then the following bash script can help you.
#!/bin/bash
for i in `ls`
do
if [ -d $i ];then
rm -rf $i
fi
done

Related

How to move files from subdirectory to a directory using a for loop

How do I move a file 1 directory up using loop? Can someone maybe help me out?
I tried this:
for dir in */*/
do mv "$dir"/*
done
Like that:
for dir in */*/
do
echo mv "$dir"* "${dir%/*/}" # Drop the echo after tested it
done
Note that this will move all files and directories under $dir to the parent directory of the $dir. If you want to move a specific file, replace "$dir"* with "$dir"file where file is the filename of the specific file. Also be very careful where and how you use this code after echo removed (running in the wrong directory would be catastrophic)

Prevent mkdir -p from overwriting directories and cp from overwriting files

I have a shell script and I want to use it for making directories and files in those direcctories. It is extremely important that this script does not overwrite or delete any files or directories. So I want to make a directory which is one directory above from the directory where the script is in. In this directory I want to make two subdirectories and in one of the subdirectories I want to copy 2 pre-existing files that have some text in them.
Files file1 and file2 are always going to be in same directory as this script and they always have same contents in them and it is very important that the contents do not change. I have multiple structures like this, they just have different names.
So I tried this:
#! /bin/bash
echo "Enter directory name"
read dirname
mkdir -p ../$dirname/{dir1,dir2}
cp file1 file2 ../$dirname/dir2
But if dirname already exists, this script overwrites it and also overwrites all the contents in it. Then I tried this:
#! /bin/bash
echo "Enter directory name"
read dirname
if [ -d $dirname ]
then
echo "directory already exists"
else
mkdir -p ../$dirname/{dir1,dir2}
cp file1 file2 ../$dirname/dir2
fi
But also this script overwrites everything. How can I make this script so that if dirname already exists, the script does not create new directories and it does not copy any files in any directory, i.e. it does not do anything?

How to copy 2 directories in linux overwritting only older files in bash

I wrote a such script in bash
#!/bin/bash
TEMP="/home/pi/project/temp/"
TARGET="/home/pi/project/deployed/"
cp -au $TEMP/. $TARGET
I figured out how to copy files but it takes so long time :(
I my temp directory I have changed only one file and
cp -au $TEMP/. $TARGET
is coping all files and overwriting them!
rsync would be a better option.
rsync -avzpr "$TEMP" "$TARGET"

Check if there's some file on a directory regardless of another directory inside - linux ksh

I'm trying to check if there's any file on a directory but i need it to be independent of the fact that can or cannot be another directory inside.
I'm using:
if [ -n "$(ls -A /unload/ebia 2>/dev/null)" ]
then
Exists="Yes"
else
Exists="No"
fi
echo "is any file inside $PATH ? $Exists."
If there's nothing on $PATH, it say no, and if there's any file inside, it say yes (it's correct), but if i create a directory inside $PATH, it keeps answering yes instead there's no file. How to avoid this?
use find:
if [ -n "$(find /unload/ebia/ -type f)" ]
then
Exists="Yes"
else
Exists="No"
fi
finds -type f switch only searches for files and skips directorys

sh - Delete Directory Whenever it Appears

In my application, whenever I open VS2013 there is an annoying folder being generated which causes my whole build to fail. The folder has some old dependencies which I do not want. I couldn't be bothered to find out why it is appearing there: I just want it gone when VS2013 starts up.
I need a simple script to delete it whenever it appears. To remove a directory we do:
rm -rf FolderThatWontGoAway
But is there way to magically tell a sh script to delete something when it shows up in the file system? Something like:
if [[ -z $(find -name "FolderThatWontGoAway") ]];
then
echo "Folder is not here."
else
#remove!
rm -rf FolderThatWontGoAway
fi
Note: A cron would not be suitable because I open and close VS2013 too erratically to know how frequently this job should run.
You could do this using inotifywait. Something like this:
inotifywait -m -e create . |
while read dir ev file
do
if [ -d "$file" ] && [ "$file" == "FolderThatWontGoAway" ]
then
rm -rf "$file"
fi
done
This will monitor the current directory for "create" events. If a new directory is created with the name "FolderThatWontGoAway", it will remove the folder and its contents.

Resources