How to delete all files in one particular directory - linux

In Linux, how can I delete all files in particular directory? For example /home/xd/karthik is my path; I want to delete all files in the above directory, if the disk usage exceeds 90%. How can I write a script for that?

rm /path/to/directory/*
add rm -r to remove the file hierarchy rooted in each file argument.
dont need script just basic shell command

Related

How to delete files from specific folders in linux?

So, I have a requirement to delete all files from specific folders within a directory. These are folders that end with "-outputs" in their names and I need to delete all files in those particular folders.
Is there a command in linux that lets you do that?
You can execute the following command:
rm $YOUR_PATH\*-outputs
Change $YOUR_PATH to the path where the files are located.
If you want to ignore nonexistent files and arguments, and avoid the prompt, you can use the -f option.
rm -f $YOUR_PATH\*-outputs
You can see the different rm options here:
rm Linual manual page

How to make original file not delete when using symbolic link linux

I want to ask when I delete file in folder symlink target, is it possible make the file is still available in original file?
I use ls -n /folder /target but when I delete file in /target , in original folder deleted too.
Remove Symbolic Links with rm
rm symlink_name
Remove Symbolic Links with unlink
unlink symlink_name
Unlink command and rm command without -R option doesn't delete regular directory. The two commands delete symbolic link from directory because it's considered as a file so, when using the rm or unlink command to remove a symbolic link from a directory, make sure you don’t end the target with a / character because that will create an error
Please read more in documentation, man pages etc.
As I understood question - you should remove symlink to directory, but make symlink for each file in directory to target directory. So you will have two normal directories and lot of symlink for files.
Only this way you can delete one particular file in target directory and still have it in original directory
If you want to make it work both ways - to delete file in original directory and still have it in target - make non symlink but hard link with ln for each file.

How to delete .jpg file in linux console

By accident I named my image file wrong so I wanted to delete it. So I typed the following in my linux console:
rm localapps\logo.jpg
But it didn't work.
Then I wrote
rm *.jpg
then it worked. Simple question. Why did the first not work , even I know that is the way to delete files in linux?
We would need the output of the commands you are running. You typically have no output when the command succeeds.
It is also important for you to notice that in linux, the / character is used to denote directories, and not \, which is actually typically the escape character.
In a terminal is also very important for you to notice in which directory are you working and what is the relative path to the file you want to refer to. You can find this out with the command pwd that stands for print working directory.
You would see something like
your-box:~ blurry$ pwd
/home/blurry
your-box:~ blurry$
This said, when you type
rm localapps\logo.jpg
since \ is a escape character, this is interpreted as
rm localappslogo.jpg
this means, it is looking for the file named localappslogo.jpg in the current directory (/home/blurry/localappslogo.jpg).
I assume that file does not exist, then, it will output something like:
rm: localappslogo.jpg: No such file or directory
when you type
rm *.jpg
this code removes any file ending in .jpg in the current directory. So notice that if you were trying to delete a file that was in the localapps folder, you should use instead
rm localapps/logo.jpg
But this is always assuming that the relative path to your image is localapps/logo.jpg.
You can also change directory then delete the file like this,
cd localapps
rm logo.jpg

How to recursively delete files and folders under a certain directory using command line?

I am a beginner of Linux system. Now I have a butch of files and folders want to be deleted. How can delete them one time using some command line?
Firstly I think you need rm command, then since you want to recursively delete the files or folders, you have to choose the option -r, -r means recursively delete something. Below is the link for details using rm command in Linux system.
https://www.computerhope.com/unix/urm.htm

Recursively copy contents of directory to all target directories

I have a directory containing a set of subdirectories and files. I need to recursively copy all the content of this directory to all the subdirectories of another directory, also recursively.
How do I achieve this, preferably without using a script and only with the cp command?
You can write this in a script but you don't have to. Just write it line by line in the terminal:
# $TARGET is the directory containing subdirectories where you want to STORE the copies
# $SOURCE is the directory containing the subdirectories you want to COPY
for dir in $(ls $TARGET); do
cp -r $SOURCE/* $TARGET/$dir
done
Only uses cp and runs on both bash and zsh.
You can't. cp can copy multiple sources but will only copy to a single destination. You need to arrange to invoke cp multiple times - once per destination - for what you want to do; using, as you say, a loop or some other tool.
The first part of the command before the pipe instruct tar to create an archive of everything in the current directory and write it to standard output (the – in place of a file-name frequently indicates stdout).
tar cf - * | ( cd /target; tar xfp -)
The commands within parentheses cause the shell to change directory to the target directory and untar data from standard input. Since the cd and tar commands are contained within parentheses, their actions are performed together.
The -p option in the tar extraction command directs tar to preserve permission and ownership information, if possible given the user executing the command. If you are running the command as superuser, this option is turned on by default and can be omitted.
Also you can use the following command, but it seems to be quite slower than tar;
cp -a * /target

Resources