Tag Info

Hot answers tagged

43

The correct syntax in bash is the following: rm /tmp/!(lost+found) As @goldilocks wrote in the comments, the original command makes an expansion on the query (it deletes all the files in the /tmp folder, then goes on, and deletes all the files in the current working folder, in your case the home folder). You can try to check if you can recover some of ...


32

As long as you don't move the file across file-system borders, the operation should be safe. This is due to the mechanism, how Ā»movingĀ« actually is done. If you mv a file on the same file-system, the file isn't actually touched, but only the file-system entry is changed. $ mv foo bar actually does something like $ ln foo bar $ rm foo This would ...


26

The !(lost+found) in your rm command was probably the fatal mistake: 1978 rm -rf /tmp/* !(lost+found) 1979 sudo rm -rf /tmp/* !(lost+found) I don't know exactly bash is doing with that, but this command prints everything in my /tmp and also everything my current directory, which is currently ~: echo /tmp/* !(lost+found)


21

sockets use different APIs That's not entirely true. There are some additional functions for use with sockets, but you can use, e.g., normal read() and write() on a socket fd. how does this "Everything is a file" apply here? In the sense that a file descriptor is involved. If your definition of "file" is a discrete sequence of bytes stored in a ...


19

Jim's answer explains how to test for a symlink: by using test's -L test. But testing for a "hard link" is, well, strictly speaking not what you want. Hard links work because of how Unix handles files: each file is represented by a single inode. Then a single inode has zero or more names or directory entries or, technically, hard links (what you're calling ...


15

You can use file tool: $ file file.png file.png: PNG image data, 734 x 73, 8-bit/color RGB, non-interlaced $ mv file.png file.txt $ file file.txt file.txt: PNG image data, 734 x 73, 8-bit/color RGB, non-interlaced


11

An example: $ touch f1 $ ln f1 f2 $ ln f1 f3 $ ln -s f1 s1 $ ln -s f2 s2 $ ln -s ./././f3 s3 $ ln -s s3 s4 $ ln s4 s5 $ ls -li total 0 10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 f1 10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 f2 10802124 -rw-r--r-- 3 stephane stephane 0 Nov 12 19:55 f3 10802345 lrwxrwxrwx 1 stephane stephane 2 Nov 12 ...


10

You can use a vi script: $ vi test.txt -c '%s/aaa/NNN/ | wq' $ cat test.txt NNN NNN bbb ccc ddd You're simply automating what would normally be entered when using vi in command mode (accessed using Esc: usually): % - carry out the following command on every line: s/aaa/NNN/ - subtitute aaa with NNN | - command delimiter w - write changes to file q - ...


10

"Everything is a file" is just an overstatement. It was novel in 1970s and it was a primary distinguishing characteristic of UNIX. But it's just a marketing concept, not a real foundation of UNIX, because it's obviously not true. It's not beneficial or sensible to treat EVERYTHING as a file. Is CPU a file? Does your program read() a CPU to get a new ...


9

Another option - ed line editor: ed -s test.txt <<< $',s/aaa/NNN/g\nw'


9

Using the -h and -L operators. -h file true if file is a symbolic link -L file true if file is a symbolic link http://www.mkssoftware.com/docs/man1/test.1.asp According to this SO thread, they have the same behavior, but -L is preferred.


8

If you are using bash: shopt -s extglob rm !(file1|file2|file3) After bash manual: !(pattern-list) matches anything except one of the given patterns and pattern-list is a list of one or more patterns separated by a |.


8

Some reasons I can think about why they used find + xargs: Handling the case when you have too many cache files, leading to an error if you run only one rm command. Globbing * does not expand hidden files. Working recursively. But this find + xargs is not efficient, since when they didn't add any filter, so find result will contain directories along with ...


7

Since you say you're using node.js, I assume you'd be using fs.rename() (or fs.renameSync()) to rename the files. This node.js method is documented to use the rename(2) system call, which does not touch the file itself in any way, but merely changes the name under which it is listed in the file system: "rename() renames a file, moving it between ...


7

Using sponge: #!/bin/bash pattern='aaa' replacement='NNN' while read -r line do printf '%s\n' "${line//$pattern/$replacement}" done < "${1}" Call with: ./script.sh test.txt | sponge test.txt


7

With the zsh shell: tail -n 5 ./*.aff(D.om[1]) With other shells, it's quite difficult to come up with something reliable if you don't want to make assumptions on what file names may contain. For instance, the bash equivalent, if you're on a recent GNU system would be: find . -maxdepth 1 -name '*.aff' -type f -printf '%T@:%p\0' | sort -rzn | sed -zn ...


7

There are a few differences in the behavior of the command lines: The find command line would delete files recursively in subdirectories, the rm command line wouldn't. You need to consider whether or not you want to recurse. The find command line would delete all files, if possible. The rm command line might skip files based on the shell's settings like ...


6

Yes you can consume all the inodes of a system. They are a limited resource just like diskspace is, and they're pre-allocated when you perform a mkfs.ext4, for example. You can use tools such as tune2fs -l <device> or df -i <path> to see how many are allocated and used. Example $ df -i / Filesystem Inodes IUsed IFree IUse% ...


6

If there is no built-in capability in the program that you use, to overlay new information in some way over a base file, you have to resolve this on the filesystem level, transparently to the application using the file. Because of your space requirement a revision control system would not suffice, although it provides you with multiple versions. One thing ...


6

Directory permissions: The write bit allows the affected user to create, rename, or delete files within the directory, and modify the directory's attributes The read bit allows the affected user to list the files within the directory The execute bit allows the affected user to enter the directory, and access files and directories inside The sticky bit ...


5

Linux normally doesn't do any locking (contrary to windows). This has many advantages, but if you must lock a file, you have several options. I suggest flock: apply or remove an advisory lock on an open file. This utility manages flock(2) locks from within shell scripts or from the command line. For a single command (or entire script), you can use ...


5

Make a subdirectory tmp, move all all the files that you want to keep to that directory and do a rm -f * afterwards. That will not affect the tmp directory. After that just do: mv tmp/* . rmdir tmp (Assuming none of the files you moved starts with a dot). This is one of the few cases where it makes sense to use the mouse and a file browser like Nautilus ...


5

If you stat a socket, you will see that it has an inode number and other characteristics of regular files, so I would classify it as a file on the filesystem. Example: # file live live: socket # stat live File: `live' Size: 0 Blocks: 0 IO Block: 4096 socket Device: fc03h/64515d Inode: 198817 Links: 1 Access: ...


5

Assuming file names don't contain newline characters and that all the *.aff files are regular files: ls -t1d -- *.aff | head -n 1 gives you the name of the most recently modified .aff-file. If you want the last 5 lines just do: tail -n 5 -- "$(ls -t1d -- *.aff | head -n 1)"


4

This is due to the way you're using inotifywatch, and the way the tool itself works. When you run inotifywatch -r /tmp, you start watching /tmp and all the files that are already in it. When you create a file inside /tmp, the directory metadata is updated to contain the new file's inode number, which means that the change happens on /tmp, not /tmp/test-1. ...


4

You can try imagemagicks identify command: http://www.imagemagick.org/script/identify.php Example: $ identify rose.jpg rose.jpg JPEG 640x480 sRGB 87kb 0.050u 0:01 Hope it helps.


4

Here's a quick Python program that should output your desired schema, using recursion. Should work in both Python 2 and 3 (although I only tested on 2). The first argument is the directory to descend into, or by default, the script will use the current directory. #!/usr/bin/env python import os import errno def path_hierarchy(path): hierarchy = { ...


4

Files are inodes. However, you can create files that are not linked to any directory and still use an inode. For instance: zsh -c 'repeat 1000 ((repeat 1000 {exec {fd}> file; rm file} exec sleep 1000 >&-) | cat)' would (eventually) use up 1,000,000 inodes. Those inodes would be of deleted files that have not been reclaimed yet since ...


4

If you are using bash or ksh, you can use pattern substitution for shell variables. Note however, that basic shell globs are less powerful and extended shell globs have some features that sed doesn't and vice versa. For more details, see 'Parameter Expansion' in man 1 bash: t=$(< test.txt); printf '%s\n' "${t//aaa/NNN}" >test.txt Extended shell ...


4

I don't know of any place where the kernel exposes the filenames associated with the blocks that it has cached. According to this answer http://stackoverflow.com/a/4941371 The best you could probably do even with a custom kernel module would be to get a list of inodes and devices. From there you would still likely need to walk the filesystem looking for ...



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