New 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 ...


0

I think this will do what you want. The approach could, I suppose, be applied to as many utilities as you wanted. But I really wouldn't recommend that because it will break your "finger habits" when you move to a new system that you haven't yet tweaked. ls() { [[ 0 == $# ]] && set -- *; /bin/ls -d "$@"; } By extension your alias would then be ...


-1

Here's a shell function that does what you want. You can paste it directly into your terminal or add it to your shell's initialization file (e.g. ~/.bashrc for bash): function dir { if [ -n "$1" ] then ls -lartd "$@" else ls -lartd * fi }


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 ...


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.


0

I believe the question is based on a misunderstanding. The dot has a special meaning in full regular expressions: It means any character. But filename expansion in the shell uses "globs", a much simpler pattern language in which the dot has no special meaning at all. So you can use it safely: % touch foo.txt foomtxt % ls foo.txt foomtxt % ls *.txt foo.txt


0

IMO the more common answer to your exercise would be ls *[^x]txt. I'm surprised that [!x] works equally well. I'm used to ! being for bash history completion, and causing weird problems because it's still a special character inside double quotes, so I just try to avoid it! # echo 1 1 # !echo echo 1 1 # "!echo" echo 1 1 # echo "the!echo" echo theecho 1 ...


0

With a perl one-liner (reformatted for readability): perl -e 'opendir($dh, "."); while ( readdir($dh) ) {$count++}; closedir $dh; print "$count\n";' or perl -e 'opendir($dh, "."); @files = readdir($dh); closedir $dh; print $#files+1,"\n";' You can use perl functions that modify arrays like grep or ...


2

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


0

Or very simply.. ls -l | egrep -v "^\." or to iterate all directories showing filenames: ls -R | egrep -v "^\."


0

you can use find command like this : for file in $(find /mnt/thara/ -type f \( ! -iname ".*" \)) ; do ---something $file--- ; done`


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 /...


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 ...


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.


0

With zsh: setopt extendedglob # best in ~/.zshrc ls -ld -- *green*~^*red* The ~ glob operator is for except (and not). ^ is for not. So the combination is and not not, so and. IOW, that matches files with green in their names, except those that do not contain red. To also include hidden files, add the (D) glob qualifier. You can also use glob qualifiers ...


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

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 ...


0

Simple, assuming that the open command can handle multiple args: open *red* *green* Edit: sorry I was reading the question as opening (files with red in the name) and (files with green in the name); my mistake. The following uses extglob and avoids the double-count in the case of files named redgreenred: $ shopt -s extglob # turn on extended ...


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....


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 ...


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 \! -...


0

It's only safe if you use new filenames. The shell knows about filenames, not their inode, etc., and does the globbing (expansion of wildcards) before running a command. According to POSIX: 2.6.6 Pathname Expansion After field splitting, if set -f is not in effect, each field in the resulting command line shall be expanded using the algorithm ...



Top 50 recent answers are included