Tag Info

New answers tagged

1

Whenever fork() makes a new child, the file descriptors are not retained at all - they are changed. Although the file will be a duplicate, it will have a different file descriptor.


4

With the zsh shell, that's typically done with globbing qualifiers: mv -- *(.om[1,10]) /dir moves the 10 newest non-hidden regular files to /dir. With the completion system (enabled by running compinstall), you can also tell zsh to expand that list and select which ones to expand using Alt-A.


1

In: tail -n 100 file > file The shell forks a process, opens file for writing in it, with truncation (that is making it an empty file) and then executes tail in that process. To open file without truncation, you can use the <> redirection operator instead: tail -n 100 file 1<> file The problem though is that there will be no truncation ...


1

If you use your desktop and the relevant application regularly, then use find <dir in question> -atime -90 -ls. If that outputs something, then you know that some file has been used. Caveat 1: that doesn't work if your home partition is mounted with noatime. Caveat 2: If find outputs some files, it does not mean that those are really important.


0

You can do it this way: printf '%s\n' "$(tail -n 100 file)" > file


1

Of course, the moment I finally ask the question on SE, the answer comes to me. I think less does what I need, so I just write: tail -n 100 file | less > file


6

sponge from moreutils is good for this. It will: soak up standard input and write to a file You use it like this: tail -n 100 file | sponge file to get exactly the effect you want.


0

I had similar issues. Do you have Gnome, KDE or some kind of Xwindow DM?. If you do open your file broser and remove the file from there. It should work. I would like to see a solution from the command line, but in my case and after losing a lot of time trying to figure out how to remove it from the command line I found that it was as simple as removing ...


2

The following excerpt from this essay potentially explains why that directory refuses to be deleted: NFSv4 requires that all filenames be exchanged using UTF-8 over the wire. The NFSv4 specification, RFC 3530, says that filenames should be UTF-8 encoded in section 1.4.3: “In a slight departure, file and directory names are encoded with UTF-8 to deal with ...


1

It doesn't make sense if the unix file permissions disagree to the acl entry and vice versa. Accordingly, the manual page (acl(5)) says what you ask for: CORRESPONDENCE BETWEEN ACL ENTRIES AND FILE PERMISSION BITS The permissions defined by ACLs are a superset of the permissions specified by the file permission bits. There is a correspondence ...


0

Have you tried to get the inode of that file with stat: stat mike* That should give you the inode number (and other data), and then you could try to delete it.


0

You specified -or, that needs to be -o, as other have corrected. You also need parentheses I think (correction on my original post). I'd recommend using %T@ for the format specifier (seconds since 1970) which is cleaner to sort; then you can convert it using date --date="@`find . \\( -iname "*.mp3" -o -iname "*.jpg" \\) -printf '%T@\n' | sort -rn | head ...


0

Have you tried using rm -rf ./mikeaâcnt or rm -rf "./mikeaâcnt" or an absolute path? Also instead of rm, try rmdir ./mikeaâcnt.


2

Also see the following: find . -type f -mtime -1 \( -name '*.mp3' -o -name '*.jpg' \) -printf '%AY-%Am-%Ad %P \n'


5

Something like this should work: find . \( -iname "*.mp3" -o -iname "*.jpg" \) -printf '%TY%Tm%Td %TT %p\n' | sort -r This should find the files that (case-insensitively) find files ending with mp3 or jpg, print out the modification time, then sort it in reverse order. It seems to show both file-types when you run it effectively as two commands: ( find ...


4

With zsh: setopt extendedglob zmodload zsh/stat zstat -F %F +mtime -- **/(#i)*.(mp3|jpg)(Om[1]) Note that it's based on last modification time, the creation time (whatever that means) is generally not readily available on Linux. It doesn't consider hidden files. I you want them, add the D globbing qualifier above.


0

After getting the correct hex code of file / folder name (using whatever method one sees fit, I may choose ls --show-control-chars | xxd), some special construct should be used to address such characters when running under bash: rmdir $'mikea\xc3\xa2\xc2\x81\xc2\x84cnt' Otherwise backslashes are treated as vanilla backslash.


4

Advisory locking is for processes that cooperate "peacefully". The kernel keeps track of the locks but doesn't enforce them - it's up to the applications to obey them. This way the kernel doesn't need to deal with situations like dead-locks. Mandatory locking was introduced in System V Unix, but it turns out that the design was not the brightest thing. ...


11

One way to delete files/direcories like this is by their inode-reference. To find the inodes for elements in current dir: ls -i 14813568 mikeaâcnt To delete this: find . -inum 14813568 -delete


6

You should not use non-ASCII characters in the command line since as you could see, for some reason, they won't necessarily correspond to the filename (Unicode has various ways for expressing accented letters). Something like: rm -rf mike* should work since the filename is directly generated by the shell. But make sure there's only one match (do an echo ...


3

I have personally tested using find's -exec directive: $ mkdir -p mikeaâcnt $ ls mikeaâcnt $ find -maxdepth 1 -type d -empty -exec rm -rf {} + $ ls $ The folder was correctly created and correctly removed. As pointed out by @Igeorget, there's an even simpler method if you have GNU find: $ find -maxdepth 1 -type d -empty -delete I also tested this ...


2

Depending on what you want exactly to do, and the structure of your files, other possibilites may be available, such as : ls -1tq /dir/*/readme.txt | head -n 1 Returns the full name of the most recent readme.txt found in any subdirectory (not recursive) of /dir/ The usage of * as part of the path (not just as part of a filename) often being underknown, I ...


1

For more portability, you can use perl: $ perl -MList::Util=reduce -le ' BEGIN { $dir = "." } opendir DIR,$dir; print shift @{(reduce {$a->[1] > $b->[1] ? $a : $b} map {[$_,(stat($_))[9]]} grep { -d and !/^..?$/ } readdir DIR)} ' .Skype Change $dir to whatever directory you want ...


3

You don't need any elaborate pipelines for this. Moreover, you don't need to find anything - you already know where each file is, you just don't know which of them is newest. This is easily handled. To demo, here's my little test: mkdir ./dir1 ./dir2 ./dir3 for d in 1 2 3 do touch ./dir$d/samefile sleep 1 done That creates the test set. Now, which ...


0

Short answer: look into man find and the 'time' options. This will locate the file(s), then use the -printf option to display the path of the file(s), which then can be used for cd <path> One way to use find and extract DIR of the last accessed file (within the last 24h) in current dir and its sub directories lastAccDir="$(find 2>/dev/null . -type ...


1

In POSIX terminology, a filename is the name of a directory entry. It consists of a non-empty sequence of bytes other than / or null. The term “pathname component” is synonymous with “filename”. A pathname is a string that can contain any non-null byte and that designates a way to locate a file. A pathname consists of a series of filenames, in which all but ...


2

cat /dev/null is a no op as it outputs exactly nothing. A simpler way to blank a file's content is then to redirect the null command to it that way: : > file or even, with most shells, only use a redirection without specifying any command: > file The fact the reported size by ls is still high is just due by the writing process seeking to the ...


3

See the Wooledge wiki on tests and conditionals: -w FILE: True if the file is writable by you. So, you could test it with: [[ -w "$file" ]] If you aren't using bash, you could equally use [ -w "$file" ]


3

Assuming you meant to say cat /dev/null > file_log.txt the answer is that the process that has the file open for writing did so without O_APPEND, or it sets the offset into the file arbitrarily, in which case a sparse file is created. This is a file that contains "holes", i.e. the system "knows" that there are large regions with zeroes, which are not ...


2

cat /dev/null file_log.txt This only made cat read /dev/null and immediately read file_log.txt and output the result to stdout, your screen. This won't delete anything, at all. If you want to test out, use cat /dev/null non_existent_file and you will see that it errors out. The correct way to truncate a file, is using shell redirectors or any kind of ...


0

The other 2 answers explain the issue well - a file doesn't get "deleted" until all directory links to it and all open file descriptors to it are gone. To avoid this, it's a good habit to use > /var/log/bigfile instead of rm -f /var/log/bigfile since that just resets the content to 0 bytes instead of deleting it, and you can still see what's ...


2

It's a format specifier for the option -c rather than an option in itself e.g. $ stat -c '%a' myfile 664 or $ stat -c '%n %a' myfile myfile 664


1

In General, pathname and filenames are the same. First, read some rules for filenames from POSIX documentation: 4.6 Filenames For a filename to be portable across implementations conforming to IEEE Std 1003.1-2001, it shall consist only of the portable filename character set as defined in Portable Filename Character Set. The hyphen ...


4

Exactly. Files are tri-partite. The content, that is, a flat array of bytes, written somewhere on a disk or generated on-the-fly. The index node, or inode for short, which is a data structure populated and used by the kernel. It contains all the metadata (size, permission, etc.) about the file, and also pointers to the location of the content of the file. ...


8

When you delete a file you really remove a link to the file (to the inode). If someone already has that file open, they get to keep the file descriptor they have. The file remains on disk, taking up space, and can be written to and read from if you have access to it. The unlink function is defined with this behaviour by POSIX: When the file's link count ...


6

A directory contains a list of filename ⇒ inode mappings. Your directory /home/tim includes an entry with filename tim.pdf, pointing at (say) inode 1234. How do we get at that directory? Well, a directory is really a special kind of file that contains those entries. We can find it the same way we find other files, by looking in its parent: /home will have ...


4

You can add the file you want to /etc/skel directory. $ sudo touch /etc/skel/test.txt $ sudo useradd -m test $ ls /home/test test.txt From man useradd: -k, --skel SKEL_DIR The skeleton directory, which contains files and directories to be copied in the user's home directory, when the home directory is created by useradd. ...


1

Are you familiar with Vim? If yes, then have a look at Ranger. It's a very fast text-based file manager, and uses Vim-like shortcuts. Move around with hjkl. To bookmark current directory, press m, then the key to store the bookmark under. To access the bookmark, press ', then the key you bookmarked it under. To create a directory simply type :mkdir ...


6

Command Line Tools I use autojump myself and I also depend on many aliases for navigating at the command line, e.g.: alias b='cd -' alias c='cd ~/Dropbox/95_2014/work/code' alias d='~/Dropbox' alias lnk='cd ~/Dropnot/webs/rails_apps/linker' alias n='cd ~/Dropnot' alias play='cd ~/play/' alias q='cd ~/Dropbox/95_2014/work/code/ruby__rails/ruby/ruby_quiz' ...


3

As for a GUI solution I use and suggest Thunar (it's of course available for other desktops too). It's fast, lightweight and stable, it's memory footprint is almost unnoticeable (~70 MiB on 30+ tabs on two separate windows). Some of its features include: Tab navigation. Drag/drop Bookmarks. Select files by pattern (Ctrl+S). Batch file and directory ...


5

Personally, I have never understood the use of fully-fledged file managers. I deeply prefer to use coreutils for file management. As a result, my solution for this would be to suggest a directory management utility. There are a myriad of these, and I have never personally found a use for them so I can make no personal recommendation. But, below are a few ...


0

Maybe it can get shorter exts=( *.jpg *.png *.gif ); printf "There are ${#exts[@]}" extensions;


4

I'd suggest a different approach, avoiding the possible word-splitting issues of ls #!/bin/bash shopt -s nullglob for ext in jpg png gif; do files=( *."$ext" ) printf 'number of %s files: %d\n' "$ext" "${#files[@]}" # now we can loop over all the files having the current extension for f in "${files[@]}"; do # anything else you like with ...


2

My approach would be: List all files in the directory Extract their extension Sort the result Count the occurrences of each extension Sort of like this: ls | awk -F . '{print $NF}' | sort | uniq -c | awk '{print $2,$1}'


2

Yes, all files under /usr should be owned by root, except that files under /usr/local may or may not be owned by root depending on site policies. It's normal for root to own files that only a system administrator is supposed to modify. There are a few files that absolutely need to be owned by root or else your system won't work properly. These are setuid ...


3

Edit: See Gilles' answer for a way to fix the permissions and ownerships instead of trying to adjust based on my (for you probably incomplete) list. There are a few files that are normally owned by another group. Excerpt from my system: $ lsb_release -d Description: Debian GNU/Linux 6.0.9 (squeeze) $ find /usr/bin -not -group root -exec ls -g {} \; | ...


1

In order to connect to a server via ssh and run a specific command, all you need is ssh "command" In your case, you want to copy a file (it overwrites by default) so you want the cp command. It works like cp /path/to/original /path/to/copy. Now, you said in your comments that trying this gives you a permission denied error. This means that you will ...


0

Without sudo the command ssh server "cd path/to/directory && cp image1.png image2.png" doesn't have privileges to chmod the permissions. But with sudo it would, but being run after ssh, it never gets password input for it on the remote server, so the solution is use -S and pipe a password for sudo as follows: ssh server " cd path/to/directory ...


5

ssh host "cd path/to/directory && cp image1.png image2.png" The && is safer than ; in case the cd fails, e.g. because of a typo: in such a case, the cp won't be executed instead of possibly copying a wrong file.


1

While Michael Homer already wrote what happened, here's why it happened (given your comment on his answer I think you already know, but others coming across this question might not). The command you issued was ls -al /usr/lib/*valgrind* The stars are interpreted by the shell even before ls is executed, by replacing it with a list of filenames matching ...



Top 50 recent answers are included