Hot answers tagged

31

You only need the shell for this job. POSIXly: for f in *.png; do printf '%s\n' "${f%.png}" done With zsh: print -rl -- *.png(:r)


20

ls -1 | sed -e 's/\.png$//' The sed command removes (that is, it replaces with the empty string) any string .png found at the end of a filename. The . is escaped as \. so that it is interpreted by sed as a literal . character rather than the regexp . (which means match any character). The $ is the end-of-line anchor, so it doesn't match .png in the ...


13

If you just want to use bash: for i in *; do echo "${i%.png}"; done You should reach for grep when trying to find matches, not for removing/substituting for that sed is more appropriate: find . -maxdepth 1 -name "*.png" | sed 's/\.png$//' Once you decide you need to create some subdirectories to bring order in your PNG files you can easily change that ...


9

Another very similar answer (I'm surprised this particular variant hasn't appeared yet) is: ls | sed -n 's/\.png$//p' You don't need the -1 option to ls, since ls assumes that if the standard output isn't a terminal (it's a pipe, in this case). the -n option to sed means ‘don't print the line by default’ the /p option at the end of the substitution means ...


8

I'd go for basename (assuming the GNU implementation): basename --suffix=.png -- *


5

You can use only BASH commands to do that (without any external tools). for file in *; do echo "${file%.*}"; done This is usefully when you're without /usr/bin and works nice for filenames like this.is.image.png and for all extensions.


4

wasn't it enough? ls -1 | sed 's/\.png//g' or in general, this ls -1 | sed 's/\.[a-z]*//g' will remove all extensions


3

just use the stat tool: stat filename with this you can see all options, so with -f you could specify an own format: man stat


3

Besides stat (Linux-specific), there are tools which allow you to do this as a side effect. The tar program, for example can do this: tar cf - filename | tar tvf - For example $ tar cf - foo |tar tvf - rwxr-xr-x 1021/1021 18 Jan 13 21:40 2016 foo Using the special "-" like that is reasonably portable (it works with AIX, HPUX, Solaris, Linux and ...


3

dirname of file is missing in first part, try grep -w 'sucessfully completed.' "/var/log/folder/$(ls -1rt /var/log/folder | tail -n1)" do not try ... unles there is no dir in /var/log/folder/ grep -w 'sucessfully completed.' "$(ls -1rt /var/log/folder/* | tail -n1)"


2

Use rev: ls -1 | rev | cut -f 2- -d "." | rev rev reverses all the strings (lines); you cut everything after the first '.' and rev re-reverses the remnant. If you want to grep 'alma': ls -1 | rev | cut -f 2- -d "." | rev | grep 'alma'


2

according to your comment " I have a text file where where all the names are listed without the extension. I want to make a PHP script that compares the text file with the folder to see which file is missing " : for file in $(cat yourlist) ; do [ -f "${file}.png" ] || { echo "$file : listed in yourlist, but missing in the directory" } done #assumes ...


2

Your shell should be able to do the filtering: ls *.pdf | wc -l or you have to make sure you match the end of filenames: ls | grep "*\.pdf$" | wc -l (notice the dollar sign). Note: both of these will also match directories ending in ".pdf", if any. Note 2: ls should behave as if you gave it option -1 as soon as you pipe its output. Otherwise, add ...


2

As steeldriver commented, the easiest way is to use ls's -d flag: -d, --directory list directory entries instead of contents, and do not dereference symbolic links


2

It is not safe to parse ls or to pipe find[1,2] It is not safe to parse (and to pipe) the output of ls or find, mainly because it possible to find in the file names non usual characters as the newline, the tab... Here a pure shell cycle will work[cuonglm]. Even the find command not piped with the option -exec will work: find ./*.png -exec basename {} ...


1

Brace expansion happens before variable expansion, so there's no way to use variables in it. You can use seq instead: seq -f foo_%03.0f.nc $ns $ne


1

No, ".pdf" macthes way too much, e.g foo.pdfa and bpdf. Furthermore, even if you don't have files wrongly matching, wc without options outputs the number of lines, words and bytes in the input, so you would get two numbers more than you're interested in. If you want grep in the mix, you could do ls | grep -E "\.pdf$" | wc -l, but unless you have a lot of ...


1

You should use ls | grep ".pdf" | wc -l The -l parameter will count only the number of resulted lines, while without the -l you would get other counts as well, like newline, word, and byte count. Note that this will count filenames (and folders as well) which contain the ".pdf" chain of characters. To count only files ending with .pdf, you'd better ...


1

In my opinion its a bad practice to try to mount a backup filesystem at startup because if something going wrong (rm -rf / [enter] ops), the data on backup probably will be deleted together. So if you are using a script to do this, mount inside the script, or inside cron, and umount when done. With that your startup will not hangs anymore, and you will ...


1

A simple shell line (ksh, bash or zsh; not dash): set -- *.png; printf '%s\n' "${@%.png}" A simple function (from No Extension): ne(){ set -- *.png; printf '%s\n' "${@%.png}"; } Or a function that remove any extension given (png by default): ne(){ ext=${1:-png}; set -- *."$ext"; printf '%s\n' "${@%.${ext}}"; } Use as: ne jpg If the output is an ...


1

If I knew the directory only had files with .png as an extension, I would have just run: ls | awk -F. '{print $1}' This will return the first "field" for anything where there is a filename.extension. Example: [rsingh@rule51 TESTDIR]$ ls 10.png 1.png 2.png 3.png 4.png 5.png 6.png 7.png 8.png 9.png [rsingh@rule51 TESTDIR]$ ls | awk -F. '{print ...


1

All of the above examples will tell you the size of the data on disk (i.e. the amount of disk space a particular file is using, which is usually larger than the actual file size). There are some situations where these will not give you an accurate report, if the data is not actually stored on this particular disk and only inode references exist. In your ...


1

ls -lhS sort by size, in human readable format


1

ls -l | sed 's/\.png$//' Is the most accurate method as highlighted by @roaima. Without the escaped \.png files named a_png.png would be listed as : a_.


1

I assume AIX has a perl of some sort. perl -e 'printf "%03o\n", (stat( $ARGV[0] ))[2] & 07777' /etc/hosts The stat function returns all sorts of exciting metadata about the chosen file. Here, I'm just using the third element ([2] counting from zero), which is mostly permissions. The printf "%03o\n" outputs the value of the permissions in octal (eg ...


1

This is more Linux specific and obscure (will need ACL tools installed) but the getfacl command will show output similar to this even if there are no ACLs set on a file: [root@mymachine ~#] getfacl my_file.txt #file: my_file.txt #owner: root #group: root user::rw- group::r-- other::r--



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