Argument list too long error for rm, cp, mv commands

前端 未结 27 2997
长情又很酷
长情又很酷 2020-11-22 04:50

I have several hundred PDFs under a directory in UNIX. The names of the PDFs are really long (approx. 60 chars).

When I try to delete all PDFs together using the fol

27条回答
  •  忘掉有多难
    2020-11-22 05:22

    I ran into this problem a few times. Many of the solutions will run the rm command for each individual file that needs to be deleted. This is very inefficient:

    find . -name "*.pdf" -print0 | xargs -0 rm -rf
    

    I ended up writing a python script to delete the files based on the first 4 characters in the file-name:

    import os
    filedir = '/tmp/' #The directory you wish to run rm on 
    filelist = (os.listdir(filedir)) #gets listing of all files in the specified dir
    newlist = [] #Makes a blank list named newlist
    for i in filelist: 
        if str((i)[:4]) not in newlist: #This makes sure that the elements are unique for newlist
            newlist.append((i)[:4]) #This takes only the first 4 charcters of the folder/filename and appends it to newlist
    for i in newlist:
        if 'tmp' in i:  #If statment to look for tmp in the filename/dirname
            print ('Running command rm -rf '+str(filedir)+str(i)+'* : File Count: '+str(len(os.listdir(filedir)))) #Prints the command to be run and a total file count
            os.system('rm -rf '+str(filedir)+str(i)+'*') #Actual shell command
    print ('DONE')
    

    This worked very well for me. I was able to clear out over 2 million temp files in a folder in about 15 minutes. I commented the tar out of the little bit of code so anyone with minimal to no python knowledge can manipulate this code.

提交回复
热议问题