Saturday, May 4, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 9030  / 2 Years ago, fri, august 26, 2022, 5:55:29

I've deleted a similar posting I made to clarify this issue so it's not a double post.



my /tmp folder is full of random files. My actual storage space is fine but because there literally hundreds of thousands of these little files, before long I'll get errors due to the sheer number of files.



I've tried various options like running cron and various sudo rm file* but after I installed Midnight Commander and tried to delete, I received this note:



"shell commands will not work when you are on a non-local file system"



So, how do I get local? I've been trying via the Terminal command line on Mac, then I tried all the same things via VMWare but no go. I tried straight Linux commands as well as using Midnight Commander. With the pure Linux I had no errors nor indication that rm wasn't working until I ran another ll commmand and saw the files so at least I received that note above from Midnight Commander.



My permissions are set the same as the testing/dev server which is identical to the live server where I'm having problems but its /tmp folder is clean.


More From » 12.04

 Answers
3

First, the usual permission bits for the /tmp directory are 1777. The leading 1 indicates that the directory's sticky bit has been set, which means that although multiple users may write to the directory, they retain ownership of any files written there so that only they (or root) may delete them. Hence without sudo you will only be able to delete your own files from /tmp. As well, some files in /tmp may be open and/or locked by other processes and you should not attempt to delete them unless you know the locks are 'stale' - you can check for open files with lsof e.g.



$ sudo lsof +d /tmp


to see any open files at the top level of the directory, or



$ sudo lsof +D /tmp


to show open files anywhere in /tmp and its subdirectories - see the OUTPUT section of the lsof manpages man lsof for an explanation of what the various columns mean.



The second issue you are likely facing is that although commands like rm can take multiple filename arguments (including expanded wildcards or shell globs), there is a limit to the allowed length of the argument list. For example, if we create 100,000 files in /tmp



# touch /tmp/file{000000..099999}
# ls /tmp/file* | wc -l
100000


(so far so good) but if we add a further 100,000 files, the expanded shell glob (i.e. the list of files to be deleted) becomes too long for the ls command line



# touch /tmp/file{100000..199999}
# ls /tmp/file* | wc -l
-bash: /bin/ls: Argument list too long
0


and similarly we can't delete in one go with rm



# rm -f /tmp/file*
-bash: /bin/rm: Argument list too long


The solution is to break down the wildcard list into manageable subsets - you did that manually, but it is possible to do it automatically using the xargs command e.g.



# echo /tmp/file* | xargs rm -f


You could also have used the find command with a -exec or -execdir action and the {} + argument replacement syntax - from man find



-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca-
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines.
Only one instance of `{}'
is allowed within the command. The command is executed in the
starting directory.



for example



# find /tmp -maxdepth 1 -name 'file*' -type f -exec rm -f {} +


although the xargs version will likely be faster if you only need to delete from the top level of /tmp (i.e. not from any subdirectories).



If the directory continues to fill up you should really investigate which users / processes are creating the files and why. If you changed /tmp's permissions you should reset them to 1777.


[#29001] Sunday, August 28, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ardingiba

Total Points: 497
Total Questions: 95
Total Answers: 109

Location: Gabon
Member since Sat, Jan 15, 2022
2 Years ago
;