Tag Info

Hot answers tagged

6

Option 1 - using ls: With extended bash globbing turned on (shopt -s extglob) you can do: ls !(one*)/*.png Option 2 - using ls and grep: You can combine ls with grep -v e.g. ls */*.png | grep -v "one/" Option 3 - (the best IMO) but uses find not ls: For recursive searching of all subdirectories using find find . -type f -name "*.png" -not -path ...


3

I think you are looking for the brace expansion {asd,qwe}: $ ls foo.{asd,qwe} foo.asd foo.qwe


3

You're looking for tail : ls -ltr | tail -n 1 This will display only the last line of ls -ltr's output. You can control the number of lines by changing the value after -n; if you omit -n 1 entirely you'll get ten lines.


2

I'd use this kind of construct as a starting point find / -type d -print0 | xargs -0 -I'{}' sh -c 'ls -ltr {} | tail -1' Caveat: it doesn't like empty directories (total 0 is output).


2

Probably this would be better: combination of find and shell find / -type d -print0 | while read -r -d '' dir; do ls -ltr "$dir" | sed '$!d' done find will output each directory found, using the null byte instead of a newline to separate them. This stream is fed into a while loop, using read -d '' to extract each null-delimited directory name. Then, ...


2

Run this: cp -p "`ls -dtr1 /Users/Me/Documents/Coffi\ Work/FTP\ Backup\ Shell\ Script/Original/* | tail -1`" /Users/Me/Documents/Coffi\ Work/FTP\ Backup\ Shell\ Script/Backup1/ Here we have added -d option os ls to get the absolute path. In your command, as ls is not returning absolute paths, you must run that from the source directory to get the file ...


2

It is the back tick quotes. They tell the shell to run the output of the command. e.g. `echo ls` will run ls. In your case you have asked bash to run the sql files. This is obviously not what you intended, as bash can not do this, the sql will not make sense to bash. Also, even with this fix, the script will not do what you describe. Someone else ...


2

The find version will also find files matching that name in subdirectories. Note you should quote or escape the * in the filename pattern; if the pattern matches a file in the local directory it will expand to that name and find will only find exactly that name. If it matches more than one filename then you will get an error because those multiple filenames ...


2

ls -ltr file*: This command just list the contents of the current directory in the long listing format (-l), sorted by modification time (-t) in reverse order (-r) of all files and directories beginning with file*. find ./ -name file*: That command searches trough the whole directory structure under the current working directory and all its subdirectories ...


2

The colours are set by ls, using the LS_COLORS environment variable. To change the colours, you can use dircolors. dircolors --print-database outputs the current source settings, which you can store in a file and adapt; then dircolors ${file} will output the processed LS_COLORS value for you using the settings in ${file}. Strictly speaking ls outputs ...


2

The globbing pattern would be ls foo.@(asd|qwe). This works out of the box in ksh; in bash also if "extended globbing" is activated with shopt -s extglob; in zsh if ksh-style globs are activated with setopt ksh_glob.


2

For various reasons related to whitespace issues, etc., it is not advisable to parse the output of ls. An alternative, which uses GNU versions of find, sort, sed: find . -mindepth 1 -maxdepth 1 -printf "%A@ %f\0" | sort -rnz | sed -z 's/^[0-9.]\+ //' find is, of course, much more flexible than ls when it comes to listing and filtering files, but it ...


1

Background reading: Why does my shell script choke on whitespace or other special characters?, Why you shouldn't parse the output of ls Setting IFS to a newline means that only newlines, and not spaces and tabs, will be treated as separators during the expansion of the command substitution. Your method will not support file names that contain newlines; this ...


1

Do you really mean adding * in filename? Or you mean the output of ls gives filename ending in * if it has execute permission? If only output problem of ls, you could simply solve by: replace ls to \ls, this is to use un-aliased version of ls, which doesn't output *


1

The shell will do filename expansion before either command is executed, so the results depends on what is in your current directory. To get what you want, I think you wouldwant to quote the * in the find command. check this example. $# First show all the files $# ~/tmp/stack$ find . . ./dir1 ./dir1/FileA ./dir1/FileB ./dir1/FileC ./dir1/filec ./dir2 ...


1

You cannot parse the output of ls, let alone ls -l because newline, just like space is as valid a character as any in a filename. Also, you'll need to consider symlinks that have an output like ... foo -> bar. Why would you use -l anyway if you only want the file name? Just do: for file in *; do ... done If you want to include dot files, depending ...


1

As others have already noticed, there are two problems with your approach. They have nothing to do with .zip vs .sh files, but with the names and locations of the files. You need to put double quotes around command substitution. Otherwise they break at space characters. See Why does my shell script choke on whitespace or other special characters? for more ...


1

It's because it depends from where you execute your cp. In your example, ls -tr1 /Users/Me/Whatever will return only website3.zip. If you run your cp from /tmp, then it will try to find the file /tmp/website3.zip. To let ls display the full path, you should use the wildcard *. Depending on what you want, it may be useful to specify -d option to not let ls ...


1

To "loop through a bunch of [.sql] files and get their names" you'd just do: for f in *.sql do do_whatever_with_file "$f" done If you just want to list the files: ls *.sql If you want to test whether there are *.sql files existing, for example: if ls *.sql >/dev/null 2>&1 then echo sql files existing else echo no sql files fi


1

Please take what it follows only as hints to stimulate to your fantasy. You can set up your bash function or alias with one or more of the following ideas. Text B/W As pointed out by mikeserv you can use printf %020d to introduce a big skip before the output of ls. alias Ls0='printf %050d|tr 0 \\n; ls -la' Moreover you can custom it with some ...


1

Since you asked about sed specifically, ls -ltr | sed '$!d'


1

With awk: ls -ltr | awk 'END { print }'



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