Tag Info

Hot answers tagged

7

If you want all the unique filenames, here you go: ls -1 | sed 's/\([^.]*\).*/\1/' | uniq If you want the files such that more than one of those has the same basename, then use: ls -1 | sed 's/\([^.]*\).*/\1/' | uniq -c | sort -n | egrep -v "^ *\<1\>" For filenames with multiple periods, use the following: ls -1 | sed 's/\(.*\)\..*/\1/' | uniq ...


6

No need for ls here. It's the shell that lists the directory content by expanding the *.sql glob. On a GNU or FreeBSD system: printf '%s\0' *.sql | sort -nz | xargs -r0 cat -- (using \0 instead of \n together with -z/-0 makes sure it also works with file names containing newline characters). Or if you have zsh: cat ./*.sql(.n) (The n glob qualifier ...


6

ls -A is the correct answer to your question, but not to the question you linked to (that question was about listing only hidden files and directories).


6

With this command, sudo du -csh * you are missing hidden directories, i.e. * expands to all names starting with anything but a dot (.). That means all directory names starting with a dot are not passed to the du command and their size is not taken into account. In most situations, adding .??* to the parameters would fit the needs : sudo du -csh .??* * ...


4

A solution using perl (I avoid parsing ls output, it's not designed for this task and can cause bugs): perl -E ' while (<*>){ ($full, $short) = (m/^((.*?)\..*)$/); next unless $short; push @{ $h->{$short} }, $full; } for $key (keys %$h) { say join " ", @{ $h->{$key} } if @{ $h->{$key} } > 1; ...


3

Since the only answers here either use sed or perl and regular expressions, I thought I'd be different and post something debatably simpler. for file in /path/to/your/files/*; do echo ${file%%.*}; done | uniq -d In this example, ${file%%.*} matches the file path up to the first period (.). So, 0001.tar.gz would be treated as 0001. The output would look ...


3

I would probably end up using temporary directory in this case: for file in [[:digit:]]*.png; do echo mv $file tmp/$(printf %04d $((10#${file%.png}+1))).png done The important part is 10#N which forces bash to interpret 000N as just N, otherwise leading zeros denotes octal numbers. For example: $ touch 0001.png 0002.png 0010.png 0020.png 0100.png ...


3

Just store the value of date +%Y%m%d%H%M%S in a variable: x=$(date +%Y%m%d%H%M%S) and later on mv file1 file1_$x.txt mv file2 file2_$x.txt ... or in a loop for all *.txt files for file in *.txt; do echo mv "$file" "${file%.txt}"_$x.txt; done (remove echo if you are happy with what you see on the screen)


3

When referring to $c if by filename you mean full-path to file then this job is really very easy. There are a few ways to do it. Using just POSIX shell globs and parameter expansion you can do: c='/home/user/working-root-directory/band-folder/album-name/music-file.mp3' file=${c##*/} album=${c#*"${c%/*/"$file"}"/} band=${c#*"${c%/*/"$album"}"/} ...


2

To make "playing" with variables a little shorter c=/home/user/working-root-directory/band-folder/album-name/music-file.mp3 for var in file album band do eval "$var=\${c##*/}" c=${c%\/${!var}} done echo $file $album $band music-file.mp3 album-name band-folder Other way is to use read command IFS=/ read -r band album file <<< ...


2

Bash command: for i in *; do echo "${i##*.}"; done | uniq -c


2

The solution for (1) Sort the files according to their numbers and start renaming with the one with the highest number. That makes collisions impossible. ls *.png | sort -rn | while read ...; do ... mv ...; done The solution for (2) Determine the number of digits (if it not the same for all files) and then use printf for keeping that length: printf ...


2

One way: #!/bin/bash cd ~/B for file in ~/A/* do file1=$(basename "$file") [ -f "$file1" ] && { echo "deleting $file1 "; rm -- "$file1"; } done


2

Not necessarily the answer you're looking for, but something good to know about is: sudo du -max /var/lib/jenkins | sort -n That's probably one of the commands I use most often for this sort of thing. The "-a" includes total directory size (so you'll see a directory full of small files as one big directory in the final output, as well as seeing all the ...


2

With simple -name: find /var/log -name '*.[2-9]' or for any digit: find /var/log -name '*.[[:digit:]]' or if other chars are possible after digit: find /var/log -name '*.[2-9]*'


2

ls -t | sed q | xclip for the latest file in the current directory in the buffer


2

i dont understand what you are asking but why dont you double click the word/filename that you want to use? when you double click a word it will select the whole word. You can setup breaks so like . or , or < or ( will stop the double click select. These are configureable. I dont have putty so i forget how to configure. Google putty doubleclick word ...


1

To find the filenames which ends with a number ranges from . [1 to 5]. find /var/log/ -type f -regextype sed -regex ".*\.[1-5]$"


1

ls -1 | awk -F'.' '{print $1}'|uniq -cd awk prints the first field($1) of each files with . field separator. uniq -d gives only the duplicates lines, and with -c option print the number of occurrences. $ ls -1 0001.jpg 0001.tar.gz 0001.tiff 0002.png 0002.tar.bz2 001.zip $ ls -1 | awk -F'.' '{print $1}'|uniq -cd 3 0001 2 0002


1

If you have a GNU environment, here's a robust solution which prints out the common base names, using gawk (just to mix it up): find . -maxdepth 1 -type f -printf "%f\0" | gawk 'BEGIN{RS="\0"} {sub(/\.[^.]+$/,""); if (length($0))printf("%s\0",$0)}' | sort -z | uniq -zd | tr '\000' '\n' This uses find with \0 (nul) delimited filenames, gawk with ...


1

Use xargs (assuming the GNU implementation) with a custom delimiter (assuming filenames don't contain newlines): ls -1d -- *.sql | sort -n | xargs -d "\n" cat --


1

There are a few problems with your command. You ran ln -s /media/Schijf-2/Nel/Mijn Documenten/ ./home/nel/Documenten This means "create a link called Documenten that points to /media/Schijf-2/Nel/Mijn Documenten/. Because of the space, the ln command was given Documenten and not ./home/nel/Documenten as a target. One of way of dealing with this is to ...


1

In one line grep -f <(ls "A") <(ls "B") | xargs -I'{}' rm "B/{}" but it work depends on file name only and may affect to empty subdirs. To avoid this use find -type f -maxdepth 1 instead of ls. For more secure check use @KasyA recepie.


1

In 3 lines: $ delfrom=/home/KasiyA/dirB # delete from $ cd /home/KasiyA/dirA # matched from /dirA$ find . -type f -exec cmp -s '{}' "$delfrom/{}" \; -exec echo rm -v "$delfrom/{}" \; In a test: $ ls -1 ~/dirA dupfile file1inA $ ls -1 ~/dirB dupfile file1inB /dirA$ find . -type f -exec cmp -s '{}' "$delfrom/{}" \; -exec echo rm -v "$delfrom/{}" \; ...


1

If you aren't afraid to parse ls: /bin/ls --color=no -1 | sed 's/\.[^.]*$//' | uniq -d That will fail if the file names contain new lines.


1

Using Perl's rename : -2 : $ rename -n 's@\b\d+\b@sprintf("%04d", $& - 2)@e' 0100.png 0100.png -> 0098.png +1 : $ rename -n 's@\b\d+\b@sprintf("%04d", $& + 1)@e' 0001.png 0002.png 0001.png -> 0002.png 0002.png -> 0003.png You can remove the -n (dry-run mode switch) when your tests become valids. There are other tools with the same ...


1

This works for basic file names, but will not work for files with carriage returns and probably a few other edge cases. FilesNoPath=$(find . -printf "%f\n") FilesWithPath=$(find .) oldIFS=$IFS IFS=$'\n' for filename in $FilesNoPath; do Matches=$(echo "$FilesWithPath" | grep -i "/$filename$") if [ $(echo "$Matches" | wc -l) -gt 1 ] then ...


1

find . -printf "%p %f\n" | sort -f -k2 | uniq -Di -f1 Specify your choice of starting directory for find if you don’t want to start at ..  Add -type f if you want just file names.  The find command produces a list of file (and directory) names, in directory order (i.e., random order, as far as you’re concerned).  -printf "%p %f\n" prints the full pathname ...


1

One way: find . -maxdepth 1 -type f | sort -u -t. -k3 | wc -l Explanation: find . -maxdepth 1 -type f: find all normal files in current directory sort -u -t. -k3: sort the output on 3 filed with . as a field separator and print only first of equal lines wc -l: count the lines Edit If you are using zsh shell then you can run echo ${(u)$(echo ...


1

I think something went wrong with the creation of the zip file, because when I create a zip file on Windows is has (portable) forward slashes: zip.exe -r pip pip updating: pip/ (244 bytes security) (stored 0%) adding: pip/pip.log (164 bytes security) (deflated 66%) But now that you have the files with file names that contain "paths" with backslashes, ...



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