zsh: how to delete contents in folder without deleting the folder? - linux

I am using zsh and I want to delete contents of a folder without deleting the folder itself. What is the best way to go about this?

rm -r myfolder/* will delete all files in that folder that do not begin with a dot.
Really the simplest solution is rm -rf myfolder && mkdir myfolder.

You can just use rm -r path/to/dir/*.

Related

Using rm for a file in a directory

I'm new using the linux commands and I'm having a problem using the rm command in a makefile.
The makefile is in the /project directory and I want to remove all the files with extension .o that are in the /project/src folder.
I have tried with
rm /src/ -f *.o
and
rm /src -f *.o
I do not receive any error in the console but it does not delete the files either.
Could someone please help me?
Thanks for your time!
You can delete files like this
rm -f src/*.o
-f is to force deletion, without confirmation
You can use: rm -f /projects/src/*.o

Removing file from folder in linux

Linux os
Hello
i have downloaded the file using wget. the file downloaded with some messy name like
index.html?format=csv&timezone=Asia%2FKolkata&use_labels_for_header=true.
now i am trying to remove this file using rm, but its not getting removed.
Linux os
sudo rm -f Unconfirmed\sudo rm -f Unconfirmed\index.html?format=csv&timezone=Asia%2FKolkata&use_labels_for_header=true
how can i removed it from folder.
It works as below. Make sure you have permission for that file/folder to edit(delete or update). You have used sudo that is correct.
To remove the folder with all its contents(including all interior
folders):
rm -rf /path/to/directory
To remove all the contents of the folder(including all interior folders) but not the folder itself:
rm -rf /path/to/directory/* or
rm -rf /path/to/directory/{*,.*}
if you want to make sure that hidden files/directories are also removed.
To remove all the "files" from inside a folder(not removing interior folders):
rm -f /path/to/directory/{*,.*}
Where:
rm - stands for "remove"
-f - stands for "force" which is helpful when you don't want to be asked/prompted
if you want to remove an archive, for example.
-r - stands for "recursive" which means that you want to go
recursively down every folder and remove everything.
rm -rf "yourfilename" put double quotes over your file name.
Also check if you have permission to delete this file.
make notifies you of the deletion by printing an rm -f command that specifies the file it is deleting.
Eg:
Here MergeSort is the file that I need to remove.

Unable to delete folder in linux with special character

I have a folder which is:
/var/www/xxx/html/'.
It was created accidentally. I try to delete it but it comes up,
No such files or directory
I have tired
rm -f /var/www/xxx/html/'.
rm -f "/var/www/xxx/html/'."
rm -f /var/www/xxx/html/*'*.
It is because of the '.
I have been looking around for a few hours but no joy.
cd /var/www/xxx/html
rm -rf \'.
worked for me.
Try this :
rmdir /var/www/xxx/html/\'

Linux Copy a directory but with a different name?

I have a directory that I want to copy all of it, but to a directory with a different name.
Example:
/Home/user/DirA-Web
copy its contents to (but it needs to be created)
/Home/user/version1/DirB-Img
/Home/user/version2/DirB-Img
I could always copy it and the rename it, I suppose.
Edit: I currently rsync the directories to the desired location and them mv in a for loop to rename them. I am looking for something cleaner.
If the directory
/Home/user/version1/
exists, a simple cp will do:
cp -r /Home/user/DirA-Web /Home/user/version1/DirB-Img
If not, you need to use mkdir beforehand, because cp has no option to
create your target directory recursively:
mkdir -p /Home/user/version1/DirB-Img && cp -r /Home/user/DirA-Web /Home/user/version1/DirB-Img

CentOS: Copy directory to another directory

I'm working with a CentOS server. I have a folder named test located in /home/server/folder/test. I need to copy the directory test to /home/server/. How can I do it?
cp -r /home/server/folder/test /home/server/
To copy all files, including hidden files use:
cp -r /home/server/folder/test/. /home/server/
As I understand, you want to recursively copy test directory into /home/server/ path...
This can be done as:
-cp -rf /home/server/folder/test/* /home/server/
Hope this helps
This works for me.
cp -r /home/server/folder/test/. /home/server
For copy directory use following command
cp -r source Destination
For example
cp -r /home/hasan /opt
For copy file use command without -r
cp /home/file /home/hasan/

Resources