Tag Info

Hot answers tagged

77

The reason why this is permitted is related to what removing a file actually does. Conceptually, rm's job is to remove a name entry from a directory. The fact that the file may then become unreachable if that was the file's only name and that the inode and space occupied by the file can therefore be recovered at that point is almost incidental. The name of ...


22

This can easily be done using ImageMagick identify -format '%n %i\n' -- *.gif 12 animated.gif 1 non_animated.gif identify -format %n prints the number of frames in the gif; for animated gifs, this number is bigger than 1. (ImageMagick is probably readily available in your distro's repositories for an easy install)


16

I haven't done much fuzz testing either, but here's two ideas: Write some zeroes into the middle of the file. Use dd with conv=notrunc. dd if=/dev/zero of=file_to_fuzz.zip bs=1 count=1 seek=N conv=notrunc Using /dev/urandom as a source is also an option. Alternatively, punch multiple-of-4k holes with fallocate --punch-hole. You could even fallocate ...


12

In bash (version 3.0 (2004) and above), ksh (since ksh93r (2006)) and zsh (version 5.0.6 (2014) and above): touch {a..z} (note that only zsh supports characters other than ASCII letters and digits, none goes as far as perl's .. operator which inspired those shells operators). With other zsh version (since 2.2 (1992)): setopt braceccl touch {a-z}


10

You should not parse xml with sed, use an xml parser like xmlstarlet instead. For your task it would be: xmlstarlet ed -O --inplace --insert "/book" --type attr -n Book_Width -v A xml_file The file content is then: <book name="Sed tutorial" price="250" Book_Width="A"/> The ed means edit mode to edit the xml tree -O omits the xml tag We want to ...


10

Use this with bash: find $1 -name "* *.xml" -type f -print0 | \ while read -d $'\0' f; do mv -v "$f" "${f// /_}"; done find will search for files with a space in the name. The filenames will be printed with a nullbyte (-print0) as delimiter to also cope with special filenames. Then the read builtin reads the filenames delimited by the nullbyte and ...


8

Using rename find . -type f -name "* *.xml" -exec rename "s/\s/_/g" {} \; or with $1 find "$1" -type f -name "* *.xml" -exec rename "s/\s/_/g" {} \; Using mv find . -type f -name "* *.xml" -exec bash -c 'mv "$0" "${0// /_}"' {} \; or with $1 find "$1" -type f -name "* *.xml" -exec bash -c 'mv "$0" "${0// /_}"' {} \;


8

Using exiftool: exiftool -q -if '$framecount > 1' -p '$directory/$filename' -r -ext:gif . Would report the paths of the GIF files that have more than one frame (in the current directory, recursively).


6

You could use bindfs like: $ ls -ld dir drwxr-xr-t 2 stephane stephane 4096 Aug 12 12:28 dir/ That directory is owned by stephane, with group stephane (stephane being its only member). Also note the t that prevents users from renaming or removing entries that they don't own. $ sudo bindfs -u root -p u=rwD,g=r,dg=rwx,o=rD dir dir We bindfs dir over ...


6

When you run emacs it creates a backup file, int his case test.txt~. If there was already a file with that name I suspect it deletes it and creates a new one. That new file creation is modifying the directory, and thus updating its modified and changed times. If you were, instead, to say echo new line >> blah.txt you would not be creating any extra ...


6

The other answers seems mostly concerned with hardware errors. Let me list some software-caused corruptions: LF replaced with CRLF. CR removed. (Even if not followed by LF) Extra Null bytes inserted. Extra Unicode "Byte Order Mark" inserted. Character set converted from UTF-8 to Latin-1 or vice versa. DOS EOF-character(#1A) deleted, even when not at End ...


6

if a program supports streaming I/O it can work with files more than the size of the memory, is this correct Usually yes, but not necessarily. Imagine a program, like uniq but more sophisticated, that counts up the number of occurrences of each unique line it gets on its input. Unlike uniq it outputs a running total of the number of occurrences seen so ...


6

In order to remove a file, you just need to be able to write to the directory the file is in. If you don't like this, you could set the "sticky" bit via chmod +t dir if you are on a halfway recent OS (this feature was introduced around 1986 in SunOS). If you like to be more fine grained, you need a filesystem with a modern ACL implementaion like ZFS. The ...


5

If you've a snapshot of the filesystem, or a backup, then yes. If you haven't, then no.


5

This is not about "compiling" a program, or even related to coding. This is your text editor creating a backup file. Your text editor (I assume gedit in this case, but correct me if I am wrong) seems to be configured to create a backup file by default. You should observe this behavior with any text file you edit. Check this answer for a solution.


4

rsync --size-only --times (or any other option that includes those, such as rsync --size-only --archive) does what you want. Basically, --size-only is intended for when you switch from a sync method which doesn't sync timestamps. It will transfer files that differ in size, and other than that, just transfer metadata (mtime, permissions, etc.). Note also ...


4

Use dd to truncate the file, or try a binary editor like hexer to edit and introduce some corruptions. Example of truncating file using dd Create 5MB file # dd if=/dev/zero of=foo bs=1M count=5 5+0 records in 5+0 records out 5242880 bytes (5.2 MB) copied, 0.0243189 s, 216 MB/s # ls -l foo -rw-r--r-- 1 root root 5242880 Aug 12 20:13 foo # Truncate 10 ...


4

On a GNU system, you can use this: sed -i '/^#[[:blank:]]Person/{n;s/#root:[[:blank:]]\+marc/root:\tsomeone@something.tld/;}' file It searches for a line beginning with # Person. Then switches to the next line and replaces #root:<blanks>marc with root:<tab> .... The -i flag edits the file inplace. -i, \+ and \t are GNU extensions. The ...


4

No, there is no way to undo a command(at least not universal). This is often a problem when users run rm with wrong regex, without realising that it covers more files than they would like to remove. Also, it would really be impossible to implement undoing ANY command from terminal. Imagine command that sends an e-mail, or plays some sound. There is no way ...


4

I think, if possible, it would have to involve encryption - nothing is denied to root (not memory access, not any disk access at any level, not access to mess with the clock to make it look like time has passed when it hasn't). If the data is on the disk, in a file, already, then an administrator can access that. Solution 1: Easiest/most reliable solution: ...


3

in sed "a" appends a pattern IN A NEW LINE. what you want to do is replace (substitute). Let's use a colon as separator for clarity: sed 's:\(<book.*\)\(/>\):\1 Book_Width="A"\2:' anything in \( .. \) is a pattern memorized by the order of appearance and recalled by \indexnumber , e.g. \1 will reproduce the first pattern saved. So we are ...


3

Just use grep in this folder: grep "" *.id Output: 1.id:123 2.id:13 3.id:5 4.id:87876 BTW: I often use this in proc or sysfs filesystems; cd /sys/class/net/eth0 grep "" * This gives you all infos in sysfs about the ethernet interface eth0.


3

Use join: join -j 1 -a 1 <(sort file1) <(sort file2) -j 1: the join field is the first one -a 1: print unpairable lines from file 1 sort: the files must be sorted for join


3

There is no file locking mechanism in place to protect file renaming or deletion because there is no need for it. Renaming or even deleting a file while it is open by another process, even if it actively writes and/or reads data, is harmless. The processes having the file open would see no difference and will access the original data of the renamed file ...


3

In general, a Linux kernel doesn't open files on its own behalf (rather than on behalf of processes). Even /sbin/init (or the 'init' program specified on the kernel command line) is exec()ed by a process (process 1 is constructed directly from an image inside the kernel, IIRC). At one time, kernel modules requiring firmware would directly open the firmware ...


3

Regarding user names, the kernel don't need (and don't care about) them, since it is dealing only with numerical user ids (the uid 0, a.k.a. root, may have special status). See credentials(7) & capabilities(7) Conversion from user names to user ids is done by libc functions like getpwnam(3). The libc may access files (notably /etc/passwd) for that, see ...


3

You could use pv: </mnt/nfs/image.img pv -L 5m >/dev/sda The -L flag limits the throughput to 5 megabytes per second. pv also writes to the stdout so you have to redirect to the target with >.


3

You concatenate (cat) them together, so for awk it is just one file (or standard input in this case). If you want to have desired result run instead awk -F, '{print FNR}' File1 File2


3

Use this instead: awk -F, '{print FNR}' file1 file2 The FNR variable in awk gives the number of records for each input file. But, when you use cat .. | awk awk reads for the stdin file descriptor, therefore awk sees only 1 "file". Try this to understand better (FILENAME contains the current file being processed): $ awk -F, '{print FILENAME" "FNR}' file1 ...


3

Most Unix systems don't track file creation times. They track a file's modification time, which is updated each time the file is written to. If the files are written sequentially when they are created (i.e. the first file is fully written before the second file is created) and not modified later, then the order of the modification times will be the same as ...



Only top voted, non community-wiki answers of a minimum length are eligible