What happens to an open file handle on Linux if the pointed file gets moved or deleted
If the file is moved (in the same filesystem) or renamed, then the file handle remains open and can still be used to read and write the file.
If the file is deleted, the file handle remains open and can still be used (This is not what some people expect). The file will not really be deleted until the last handle is closed.
If the file is replaced by a new file, it depends exactly how. If the file's contents are overwritten, the file handle will still be valid and access the new content. If the existing file is unlinked and a new one created with the same name or, if a new file is moved onto the existing file using rename()
, it's the same as deletion (see above) - that is, the file handle will continue to refer to the original version of the file.
In general, once the file is open, the file is open, and nobody changing the directory structure can change that - they can move, rename the file, or put something else in its place, it simply remains open.
In Unix there is no delete, only unlink()
, which makes sense as it doesn't necessarily delete the file - just removes the link from the directory.
If on the other hand the underlying device disappears (e.g. USB unplug) then the file handle won't be valid any more and is likely to give IO/error on any operation. You still have to close it though. This is going to be true even if the device is plugged back in, as it's not sensible to keep a file open in this case.
Removing part of a filename for multiple files on Linux
First of all use 'sed -e' instead of '\e'
And I would suggest you do it this way in bash
for filename in *.fasta; do
[ -f "$filename" ] || continue
mv "$filename" "${filename//test.extra/}"
done
How to remove first 16 characters of all file names in a directory?
In bash, you can run
for f in *; do mv "$f" "${f:16}"; done
to rename all files stripping off the first 16 characters of the name.
You can change the *
to a more restrictive pattern such as *.fits
if you don't want to rename all files in the current directory. The quotes around the parameters to mv
are necessary if any filenames contain whitespace.
bash's ${var:pos:len}
syntax also supports more advanced usage than the above. You can take only the first five characters with ${f::5}
, or the first five characters after removing the first 16 characters with ${f:16:5}
. Many other variable substitution expressions are available in bash; see a reference such as TLDP's Bash Parameter Substitution for more information.
Related Topics
Can't Sample Hardware Cache Events with Linux Perf
To What Extent Can One Rely on Writing to Disk When Sigterm Is Given
Stty: Standard Input: Inappropriate Ioctl for Device
How to Extract Email Headers Extending on Multiple Lines from File
Mongodb (Result= Signal, Code = Killed, Signal = Ill
Source Bashrc Doesn't Work in Cron
Error: Nvidia-Smi Has Failed Because It Couldn't Communicate with The Nvidia Driver
Tmux .Tmux.Conf Doesn't Load Properly
Linux Shell Scripting: Hex Number to Binary String
Unix/Linux Ipc: Reading from a Pipe. How to Know Length of Data at Runtime
Trying to Delete All But Most Recent 2 Files in Sub Directories
Udev Rules Are Not Working for Libusb on Ubuntu 12.04
How to Invoke Any Kernel Function
What Exactly Is Program Stack's Growth Direction