Delete by inode linux

Linux: Delete File

You can use rm command and unlink commands to remove a file or directory under CentOS/RHEL/Ubuntu or other Unix-like Linux system. On Most filesystems, if you want to delete a file, and you need to have write permission on the parent directory.

The syntax is as followss for rm command:

rm rm [options] unlink rm -rf

rm Command Usage:

rm [OPTION]. [FILE]. Remove (unlink) the FILE(s). -f, --force ignore nonexistent files and arguments, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always --one-file-system when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument --no-preserve-root do not treat '/' specially --preserve-root do not remove '/' (default) -r, -R, --recursive remove directories and their contents recursively -d, --dir remove empty directories -v, --verbose explain what is being done --help display this help and exit --version output version information and exit

Linux Delete Single File

If you want to delete a single file in your Linux system, you just need to use the rm command with your filename that you want to remove. For example, you need to remove a file called fio.txt, just issue the following command:

Linux Delete Multiple files or Directories

If you want to delete multiple files or directories in your Linux system, you can pass multiple file names to rm command. For example, you need to remove fio1.txt, fio2.txt and fio3.txt in the current directory, just issue the following command:

# rm fio1.txt fio2.txt fio3.txt
root@devops:~/osetc# ls fio1.txt fio2.txt fio3.txt root@devops:~/osetc# rm fio1.txt fio2.txt fio3.txt root@devops:~/osetc# ls root@devops:~/osetc#

Linux Delete All Files in a Directory Recursively

If you want to delete all files and subdirectories from a specific directory, you can use the rm command with -rf option to achieve the result. For example, you need to remove fio direcotry and its subdirectories in your current directory. Just run the following command:

root@devops:~/osetc# ls fio fio1.txt fio2.txt fio3.txt subfio root@devops:~/osetc# rm -rf fio root@devops:~/osetc# ls fio ls: cannot access 'fio': No such file or directory root@devops:~/osetc#

Linux Delete a File with Prompt

If you want to get a prompt for approval to delete each of the files in your system, you need to use the rm command with -i option. Just like the below command:

devops@devops-osetc:~/working$ ls fio1.txt fio1.txt devops@devops-osetc:~/working$ rm -i fio1.txt rm: remove regular empty file 'fio1.txt'? y devops@devops-osetc:~/working$ ls fio1.txt ls: cannot access 'fio1.txt': No such file or directory

Linux Delete Empty Directory

If you want to delete a empty directory, you need to use another command named rmdir to achieve the result. if you just only use the rm command, it is not able to remove a directory. Issue the following command:

devops@devops-osetc:~/working$ rm fio rm: cannot remove 'fio': Is a directory devops@devops-osetc:~/working$ rmdir fio devops@devops-osetc:~/working$

Linux Delete Files Listed in a File

If you have a text file named fio.txt that has a list of paths to various files. and you want to iterate through each line to delete the file. You can use the rm command in combination with xargs command to achieve the result of remove files listed in fio.txt file. Issue the following command:

root@devops:~/osetc# cat fio.txt /root/osetc/fio1/file1.txt /root/osetc/fio1/file2.txt /root/osetc/fio1/file3.txt root@devops:~/osetc# xargs rm -v 

Linux Delete Files with dash character

If you want to delete a file with a dash in its name (-fio.txt), you need to use the rm command with –. just like this:

Linux Delete Files by Inode Number

If your file name contain special character, such as: backslash or dash, you will fail to delete files using rm command. At this moment, you can delete these files by inode number. Firstly, you need to use ls -i command to get the inode number of the file that you want to remove.

Type the following command to get the inode number of file fio.txt:

devops@devops-osetc:~/working$ ls -i fio.txt 397287 fio.txt

From the above output, you will know that the inode number of fio.txt file is 39728.

Next, you need to use find command with delete and inum option to delete file. type:

# find ./ -inum 397287 | xargs rm -v
devops@devops-osetc:~/working$ find ./ -inum 397287 -delete devops@devops-osetc:~/working$ ls -i fio.txt ls: cannot access 'fio.txt': No such file or directory

Linux Delete Files before a Certain Date

If you want to delete files older than a certain date on Linux, you can use the find command in combination with rm command to achieve it. For example, you need to remove all files older than 10 days, then run the following command:

# find -type f ./ -mtime +10 -exec rm -rf <> \;

Источник

How to Delete Large Directory with Thousands of Files in Linux

File management is one of the common tasks that a user undertakes on a Linux system, which includes creating, copying, moving, modifying, and deleting files and directories.

This article provides a few command-line tips on how you can delete a large directory that contains thousands of files in a Linux system.

Delete Files in Linux

The most common way of deleting files on a Linux system is using the rm command, which takes the following syntax format:

$ rm [ options ] sample_file.txt

For example, to delete a text file called file1.txt, run the command:

To forcefully remove a file without being asked for permission, pass the -f flag as follows.

Delete Directory in Linux

To remove or delete a directory called sample_directory, run the following command:

The -r option recursively deletes the directory alongside all the subdirectories and files contained therein.

To delete or remove an empty directory use the rmdir command, which comes in handy when you want to remove an empty directory called test_directory as shown:

Delete a Large Directory with Tons of Files

When the rm command is executed, the filesystem only removes the link to the file, which makes the file unavailable to the user, but in the real sense, the file’s data itself remains intact on the disk.

Therefore, when the rm command is issued, only the reference to the files is removed, which frees up the storage blocks in the filesystem.

As such, there exist several avenues to delete files in Linux.

Delete Files With Inode Number in Linux

For example, you can delete a file using its inode number. You can find out a file’s inode number using the stat command as shown.

$ stat file1.txt File: file.txt Size: 4076 Blocks: 8 IO Block: 4096 regular file Device: 801h/2049d Inode: 1573697 Links: 1 Access: (0664/-rw-rw-r--) Uid: ( 1000/ tecmint) Gid: ( 1000/ tecmint) Access: 2023-05-08 12:10:55.656070248 +0530 Modify: 2023-05-08 12:10:55.656070248 +0530 Change: 2023-05-08 12:10:55.656070248 +0530

In addition, you can pass the -i flag in the ls command when listing files inside a directory.

$ ls -li 1573697 .rw-rw-r-- tecmint tecmint 4.0 KB Mon May 8 12:10:55 2023  file1.txt

To remove the file using its inode, use the find command as shown in the syntax below.

$ find /path/to/file -inum INODE_NUM -exec rm -i <> +

In our example, to remove file file1.txt that sits in the current directory, the command will be:

$ find /path/to/file -inum 1573697 -exec rm -i <> +

Hit 'y' to confirm the removal and press ENTER.

Delete Files By Inode Number

Let us now see how to delete large directories with thousands of files.

Create a Directory with Thousands of Files

The good old rm command is the fastest way of deleting a large directory with thousands of files. To demonstrate this, we will, first, create a sample directory and navigate into it.

$ mkdir test_dir $ cd test_dir

Next, we will create an insanely huge number of files, in this case, 500,000 text files using the following bash for a loop.

$ time for item in ; do touch file_name$item.txt; done

NOTE: The above command is resource intensive and, hence, consumes substantial CPU and RAM. It also takes quite some time depending on your system specifications. In my case, I’m running a VM with 4GB RAM and 3 CPUs.

Create Thousands of Files in Linux

Fastest Way to Delete Directory in Linux

The fastest way to delete a large directory is using the good old rm directory as shown below. Here, the time option displays the time taken to successfully execute the command.

Fastest Way to Delete Large Directory

From the output, you can see that it has taken roughly 6 seconds to delete the entire directory.

Delete Large Directory with Find Command

Another way to delete large directories is using the find command as shown in the following syntax.

$ time find /path/to/directory -delete

Although not as fast as the rm command it still gets the job done.

$ time find test_dir -delete

Find Command - Delete Large Directory

Delete Large Directory with Perl Command

Another approach is to use the Perl scripting language inside the directory to remove tons of files.

$ cd test_dir $ time perl -e 'for()'

Perl Command - Delete Large Directory

From the output, you can this that it took much longer to delete all the files in the directory than the previous commands that we looked at earlier.

Conclusion

There you have it. In this guide, we have looked at how you can delete large directories that contain thousands of files on a Linux system.

Источник

Delete File By inode Reference

Note: This post is over two years old and so the information contained here might be out of date. If you do spot something please leave a comment and we will endeavour to correct.

If you want to delete a file that you can't type in the name of either because the name is long and complicated, or because it is difficult to type in without causing a syntax error then here is the solution.

You first need to find the inode reference of the file. This can be done by using the command ls -li. The start of each line has a number that is specific to that file. You could use the command ls -i , but the output is a little confusing.

To delete the file use the find command with the flag -inum, followed by a pipe into the rm (remove file) command like the following.

The xargs bit is used to pass a list of the files found from the find command to the rm command.

Phil Norton

Phil is the founder and administrator of #! code and is an IT professional working in the North West of the UK. Graduating in 2003 from Aberystwyth University with an MSc in Computer Science Phil has previously worked as a database administrator, on an IT help desk, systems trainer, web architect, usability consultant, blogger and SEO specialist. Phil has lots of experience building and maintaining PHP websites as well as working with associated technologies like JavaScript, HTML, CSS, XML, Flex, Apache, MySQL and Linux.

Want to know more? Need some help?

Let us help! Hire us to provide training, advice, troubleshooting and more.

Support Us!

Please support us and allow us to continue writing articles.

Источник

how to remove a file named "?" in linux? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.

I created a file named "?", is anybody know how to delete it? It seems that ? is a special character in linux, I use Redhat as my OS. I have already tried

4 Answers 4

find the inode of the file:

then delete the file using inode:

BTW, rm ? works for me fine. here is my bash version:

# bash --version GNU bash, version 4.1.5(1)-release (i486-pc-linux-gnu) 

When you execute rm ? , it removes all the files with a single character in the directory I guess. Try touch ? , touch a and rm ? . It would delete both ? and a

BTW, rm ? works for me fine. -- That's possibly because that was the only file with a single character in your directory.

rm \? and rm "?" are both perfectly good ways to delete a file named ? . If they didn't work, and you still seem to have a file name ? , then it is most likely that the ? being shown is not really a ? , but rather the result of substituting an unprintable character with a ? . To see what the file is really called (with GNU ls ) try:

Use this rm command to remove a file named ? :

OR from another directory:

I have tried both, but in doesn't work, however in my friend's machine, the commands I've tried and yours worked out.

If filename is literal ? then this command should work. I posted it after myself testing it on OSX and Linux hosts.

I remember in older bash versions I had to use this command, but now with the bash in my (corporate) development environment, version 4.3.48 this does not work. I have had to use a plain command without escape chars, exactly like follows: rm ./?

Источник

Читайте также:  Зеркало обновлений nod32 linux
Оцените статью
Adblock
detector