Hot answers tagged

7

With zsh: rm -f pre*(OL[2,-1]) OL: reverse order by size [2,-1]: second to last only The equivalent with bash and GNU utilities would be something like: eval "files=($(LC_ALL=C ls --quoting-style=shell-always -dS ./pre*))" rm -f "${files[@]:1}" You may want to limit it to regular files, as the size for non-regular files has generally not much ...


7

You can use a combination of few utilities: stat -c '%s %n' pre_* | sort -k1,1rn | tail -n +2 | cut -d' ' -f2 | xargs rm Assuming GNU system and no unusual filenames. stat gets the filesize and name separated by space for all pre_* files sort sorts the file according to the file size, with highest sized one goes to top tail -n +2 gets the rest of the ...


5

You could use find, which (on many platforms) has an -ls option. So you could do find . -maxdepth 1 -name '*' -ls But if it is hard to remember ls -d, you may not find find an improvement.


5

Although your command probably works, here is a test case: $ ls $ echo * $(sleep 1)&touch file1 [1] 12798 $ file1 [1]+ Done echo * $(sleep 1) Note that file1 was not typed in, it was the output of the echo command. Edit: Another test run: $ ls $ touch file1 $ for i in {1..5000}; do rm * & touch file$i; wait;done|grep file ...


4

This is not safe. You have not specified what the problem is that you are trying to solve. If your problem is that you want your directory to always be there but be cleaned up from time to time, I would suggest explicitly removing files older than a check file (the sleep 1 is me being paranoid): touch regression.delete \ && find regression \! -...


4

Don't rely on wildcard matching / shell globbing but instead use the find command to find and list the files you want and send that list to tar to be archived: find /A/ -type f -print0 -name \*.log | tar -cvf /path/to/file.tar --null -T -


3

The normal behavior of bash is to do not look to the hidden files when using for. But this behavior can be changed with shopt command. To enable the scanning for hidden files with "*": shopt -s dotglob To disable the scanning for hidden files with "*" (default behavior) shopt -u dotglob So try a script like this : shopt -u dotglob for eachfile in /...


3

find . -type f -name '*red*' -name '*green*' -exec echo {} + Replace echo with vi, gvim, less or whatever command you want to open the files with. Note, some programs can not handle multiple filenames on the command line. If your chosen program is one of those, replace the + at the end of line with \;. This will run the command once for each filename, ...


2

(On the assumption that you're looking for file names that contain both the string "red" and the string "green") To pointlessly use bash to test filenames against 'red' and 'green' using the =~ regular expression match operator: for f in * do [[ $f =~ red && $f =~ green ]] && echo Bash: yes: "$f" || echo Bash: no: "$f" done To use ...


2

Assuming you did not run this command as root, I would assume the data was just copied. One quick way to tell would be to run diff for comparing the directories.


2

Other way is for eachfile in /mnt/thara/[^.]*


2

rm -rf regression/* runs in parallel with ( sleep 10 ; run_regression ). This means that you have no guarantee as to the order of things. rm -rf regression/* first collects the list of files in the regression directory, then invokes rm to delete them. This doesn't happen by magic, it's the shell doing the work as part of evaluating the command rm -rf ...


1

If what you want is a command cmd so that cmd cr* lists cron.daily itself, then that command is ls -d. If that's too much typing, you can define an alias. For example, put this line in your ~/.bashrc: alias l='ls -d' Then running l cr* will display information about cron.daily itself, while ls cr* will list the contents of the directory cron.daily (and of ...


1

I agree with the comment about the dotglob shell option. If it is unset, the behavior of the for loop is the one expected: utente@computer:/tmp/test$ shopt | grep dotglob dotglob off Let a, b, and c are normal files; .hidden1 and .hidden2 hidden files: utente@computer:/tmp/test$ touch a b c .hidden1 .hidden2 utente@computer:/tmp/test$ ls -al ...


1

With #!/bin/bash set -f mono app.exe "$@" set -f has no effect because the double-quoted argument array ( "$@" ) undergoes no further expansions. If "$@" contains an argument whose value is *, it will get through to mono app.exe unchanged. The problem is, that the shell calling this wrapper script will want to expand the asterisk, as set +f is the ...


1

Wildcard matches are sorted in lexicographic order, so 10 is between 1 and 2, not after 9. To sort matches with numbers in numeric order, use zsh and its n glob qualifier pdfjam *.jpg(on) Or (still zsh-only) set the numeric_glob_sort option: setopt numeric_glob_sort # this can go in your ~/.zshrc pdfjam *.jpg If all your files have a number of the ...


1

mv regression regression.old rm -rf regression.old & mkdir regression run_regression Rename the old regression directory, delete it in the background, make a new regression directory, and then run your program. if run_regression creates the directory itself if it doesn't exist then the third step isn't necessary. A safer version, in case regression....



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