using execv/execl to delete all files - linux

I'm trying to delete all files in folder using from c program using the following method:
execl("/bin/rm","/media/sda1/*",0,0,0,0,0,0,0,0,0);
But I get the following failure:
rm: can't remove '/media/sda1/*': No such file or directory, though there are files in this folder.
How can we delete all files or copy all files (from one folder to another) using execv family ? Does anyone have any idea ?
Thanks,
Ran

The problem is caused by the glob pattern /media/sda1/* you are using: Note the asterisk, which a shell would expand to the the list of all non-hidden files in that folder. If you are passing it directly to rm, it would attempt to delete a folder called *.
If you don't want to manually iterate over all filesinside the folder, you'll need to start the command in a shell which will expand the glob pattern for you.
You could use
execl("/bin/bash","-c 'rm -rf /media/sda1/*'",0,0,0,0,0,0,0,0,0);
... for that. A nice alternative would be to use system() which implicitly starts the command in a shell:
system("rm -rf /media/sda1/*");
More about:
glob
the function system()

Related

Databricks - How to remove files , directories based on regular expression

I had a lot of files in databricks and wanted to clean them. Some of the files having a prefix such as "tweets1*.
How could I delete the files using a prefix something like linux pattern. I applied the following command, and it didnt work.
dbutils.fs.rm("/tweets1*",recurse=True)
You can go with the classic bash.
Inside your cell type:
%sh
rm -rf path/to/your/folder/tweets1*
When I have to perform some complex operation which I already know how to do with bash I use it directly inside the cell.

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

Bash, copy files from folder to another

I have a problem with an Uni OS Course assignment.
Basically the task says:
Deliver now a file for assessment. The content of the file is: one line, containing a command that
copies all files with prefix "2016", from directory "ExercisesOS" to directory "OSLab".
Consider the current directory to be "~" when writing such command.
I have already tried with that code:
cp /ExercisesOS/2016* /OSLab
but it performs me two error.
How can I write the correct command?
You probably want to copy from the directory you are working.
To check where you are working:
$ pwd
/home/userdir
To copy from your working directory:
$ cp ExerciseOS/2016* OSLab/
mkdir OSLab && cp /ExercisesOS/2016* OSLab
This solution would assume that the directory 'OSLab' isn't already created.

Unable to remove files from directory

I am using rm to delete some files from a directory through a Perl script but it throws the error
can't exec "rm" no such file or directory.
Command goes like this :
system("rm $directory$files"); $directory$files = /var/spool/mqueue/qf*
Perl has a builtin function for removing files, unlink. The third example shows how to use it in combination with glob to delete a list of files:
unlink glob "*.bak";
or in your case,
unlink glob($directory.$files);

Resources