Tag Info

Hot answers tagged

53

I ran this: strace -o spork.out bash -c "echo 1234 >> some-file" to figure out your question. This is what I found: open("some-file", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3 No file named "some-file" existed in the directory in which I ran the echo command.


43

This is not only done in Bash, it's required by the standard. From the Single Unix Specification: Appended output redirection shall cause the file whose name results from the expansion of word to be opened for output on the designated file descriptor. The file is opened as if the open() function as defined in the System Interfaces volume of POSIX.1-2008 ...


25

Looking in the source, it does use O_APPEND. For bash 4.3.30 in make_cmd.c line 710-713 read: case r_appending_to: /* >>foo */ case r_append_err_and_out: /* &>> filename */ temp->flags = O_APPEND | O_WRONLY | O_CREAT; break;


17

You can create a 10MB gzip file like this: head -c 10M /dev/urandom | gzip -1 >10m.gz This uses urandom to get a high-entropy stream of bytes: since this is incompressible, the gzipped version will be about the same size as the input. You can then catenate copies of your gzip file together: cat $(perl -e "print '10m.gz ' x 30") >300m.gz Thirty ...


16

TL;DR find / ! -type l -print0 | sudo -u "$user" perl -Mfiletest=access -l -0ne 'print if -w' You need to ask the system if the user has write permission. The only reliable way is to switch the effective uid, effective gid and supplementation gids to that of the user and use the access(W_OK) system call (even that has some limitations on some ...


15

Let's investigate that using strace on a local (non-NFS) filesystem: $ strace -eopen -- bash -c "echo foo >> /tmp/testfile000" 2>&1 | grep /tmp/testfile000 open("/tmp/testfile000", O_WRONLY|O_CREAT|O_APPEND, 0666) = 3 $ strace -eopen -- bash -c "echo foo > /tmp/testfile000" 2>&1 | grep /tmp/testfile000 open("/tmp/testfile000", ...


10

touch creates a new, empty file if the file doesn't exist because that's what it was designed to do. The utility has to contain code to handle that case specifically. The utility appeared in Unix V7; its manual described it thus: touch — update date last modified of a file touch attempts to set the modified date of each file. This is done by ...


10

$file | grep -o executes the command specified by the value of file and pipes its output to grep. But that's clearly not what you wanted. If you want to list files that contain o You meant the value of file to be an input file for grep, not a command to execute. So you need an input redirection, not a pipe. if grep 'o' <"$file" grep reads from ...


8

Using strace touch t yields: open("t", O_WRONLY|O_CREAT|O_NOCTTY|O_NONBLOCK, 0666) = 3 It is in touch's code and I wouldn't call it a check though. The timestamp is updated by opening the file for writing and then just closing it.


7

why am I getting hangups You aren't getting "hangups" from cat(1) and tail(1), they're just blocking on read. cat(1) waits for input, and prints it as soon as it sees a complete line: $ cat /dev/stdout foo foo bar bar Here I typed fooEnterbarEnterCTRL-D. tail(1) waits for input, and prints it only when it can detect EOF: $ tail /dev/stdout foo bar ...


6

There is concept known as "UNIX-way". Each tool should perform one simple function. If one need a more complex function, he can combine smaller tools. The opposite is the monolitic design when all functionality is aggregated within one huge tool. If you want to do something complex - just write a script, invoking simple tools.


6

On Linux and recent FreeBSD, this is how: cp -al dirA dirB For a more portable solution, see answer using pax and cpio by Stéphane Chazelas


6

Solution Using Parallel You could use GNU Parallel for a compact, faster solution. find . -type d -print0 | parallel -0 cd {}'&&' <command-name> This will work absolutely fine, even for directory names containing spaces and newlines. What parallel does here is that it takes the output from find, which is every directory and then feeds it to ...


5

To extract lines that start with May 1: grep "^May 1\b" file Or: sed -n '/^May 1\>/p' file Or: awk '/^May 1\>/' file The above two assume a tool, such as the GNU awk or sed, that supports \> as a word boundary regex. The purpose of the word boundary is to prevent the regex from matching, for example, May 10. More If you are looking for ...


5

A read-only file system can only be read (therefore it is named read-only). To delete files on this file-system you have to remount it read-write.


5

rsync -avz --delete "/home/user/A" "/home/user/B"


5

The find command is powerful, but that makes it a little challenging to use. I'm pretty sure it can do what you need - this command below is "almost" what you ask for: find . -type d -execdir pwd \; But - this does not run the command in the deepest directory level - it runs in the directories in which other directories are found. So it wil run in ...


5

It sounds like you want a scoring system.  Write a script to assign a score to each line, indicating how early in the output you want to see it.  awk seems well suited to this job.  For your example: #!/bin/sh awk '{score=0} /usb/ {score=1} /Plantronics/ {score=2} {print score, NR, $0}' "$@" This assigns a score of 0 to every line by default, ...


5

Brendan Gregg's iosnoop (part of his perf-tools) will give you detailed information about an application's I/O; for example: # ./iosnoop Tracing block I/O... Ctrl-C to end. COMM PID TYPE DEV BLOCK BYTES LATms supervise 1809 W 202,1 17039968 4096 1.32 supervise 1809 W 202,1 17039976 ...


5

You're trying to execute $file. Instead, you must echo it: # ... if echo "$file" | grep 'o' ; # ... Note that the grep will already print the filename, so you should silence it (e.g. grep -q 'o' or grep 'o' >/dev/null). You're also passing -l to ls, which you don't want to do. ls -l prints the file name and attributes, and you'll be matching against ...


4

There is such a program, and it's called rdfind: SYNOPSIS rdfind [ options ] directory1 | file1 [ directory2 | file2 ] ... DESCRIPTION rdfind finds duplicate files across and/or within several directories. It calculates checksum only if necessary. rdfind runs in O(Nlog(N)) time with N being the number of files. If two (or more) ...


4

POSIXly, you'd use pax in read+write mode with the -l option: pax -rwlpe -s/A/B/ dirA . (-pe preserves all possible attributes of files (in this case only directories) that are copied, like GNU cp's -a does). Now, though standard, that command is not necessarily very portable. First, many GNU/Linux-based systems don't include pax by default (even though ...


4

chgrp is to "change the group ownership of a file or directory". Thus, you can't change user ownership with that command (use chown instead, which can change user and group ownership) If your folder fruit is in 777 mode, obviously, anybody can create a sub-folder inside it. This sub-folder will be owned by the user who created it, so in your case, the ...


4

Do not use ls. It's not recommended to use in such cases. Moreover using grep to filter according to date is not a good idea. You filename might itself contain 2012 string, even though it was not modified in 2012. Use find command and pipe its output. find . -newermt 20120101 -not -newermt 20130101 -print0 | xargs -0 cp -t /your/target/directory Here, ...


4

If you have newer files on the old disk that you want to ignore I would go about it like this Create a temporary marker file with a modified-by date that separates files I want from those I don't Copy files older than the marker file to the new location Here are sample commands for this, which assume you want to maintain any directory hierarchy from the ...


4

To find all files in the current directory and its subdirectories whose last modification time is earlier than 2015-05-28: find . ! -newermt 20150527 If you only want files from the current directory and not its subdirectories, use: find . -maxdepth 1 ! -newermt 20150527 How it works find This is one of unix's most useful commands when searching for ...


3

Filenames are used to lookup inodes. Nothing else. Inodes are the main point to reference a file. Afile may not have any data blocks at all if it is zero size or if it is small enough to fit in the blocklist portion of the inode and the filesystem has that optimization. if there are two hard links pointing at one inode it is still one file, it just has more ...


3

I'm assuming we're discussing "regular" files, not device files or unix-domain sockets or something not-so-regular like that. I would say that files have names, metadata, and data. This corresponds directly to filenames, inodes and blocks. I believe that your two, hard-linked filenames are just two names for a single file. I don't believe that under Unix ...


3

You cannot do that on the usual Linux filesystems, as it doesn't keep track of the creator of the file, only of the owner of the file. The creator and owner are usually, but not necessarily the same. If you want to find the owner of the file, you can, as Bratchley indicated, use find / -type f -user user_name to find those files and display the names. ...


3

To find files that have been modified a certain days ago, it is better to use -mmin instead of -mtime as the latter will ignore any fractional part. So, 1 day 23 hours is also treated as 1 day. From man find: -atime n File was last accessed n*24 hours ago. When find figures out how many 24-hour periods ago the file was last accessed, any ...



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