How to reverse and sort files in Linux? - linux

I'm trying to take files in a directory and reverse them and then sort them alphabetically.
So that
Cat
Dog
So the output Would be
God
Tac

If you use terminal write:
ls -r
It show you your files and directories in reversed order.
I don't know what kind of Graphical Interface you use. In Gnome you must show files as a list and sort by name (clicking it) diminishing

If you want to work with files and folder names, not to change their names, I hope this will help you:
ls your-path | rev | sort

This will do the trick in plain shell script:
(list the files of a directory, reverse their names and sort them alphabetically)
ls | rev | sort

Related

How to pass a list of files to parallel command and execute downstream command such as samtools?

I have a list of files which I want to sort and index ,i listed all those files in a text file.
/run/media/punit/data1/GSE74246/tophat_output/CMP_SRR2753096/CMP_6792.bam
run/media/punit/data1/GSE74246/tophat_output/CMP_SRR2753104/CMP_7256.bam
The above one is just a list of my data which i want to sort and index.
Now i want to use this command
ls *.bam | parallel "samtools view -b -S {} | samtools sort - {.}; samtools index {.}.bam"
Meanwhile I have also files with
.bam
extension such as unmapped.bam which i dont want to sort and index
How can i exclude those "unmapped.bam" but since i dont have those unmapped.bam in my list but still i wonder if i use parallel then would it take those sort and index...
ls *.bam | grep -v unmapped | parallel ...

how to skip the files in the specific patteren in a folder

I have one folder in this folder contain different kind of files like
qt_fact_info.d20140228
qt_fact_info_is.d20140228
qt_fact_info_bkk.d20140228
qt_fact_info_lb.d20140228
qt_fact_info_sg.d20140228
I want only qt_fact_info.d20140228 this type of files only. I don't want files matching these patterns: *_is,*_bkk,*_lb,*_sg
Please help me how to print the above required kind of files(qt_fact_info.d20140228) by using UNIX shell scripting
You can do it like this:
ls | grep -v '_is\|_bkk\|_lb\|_sg'
grep -v prints everything that does not match the pattern.
\| means or
ls | sends the output of ls to grep for filtering.

Concatenate Files In Order Linux Command

I just started learning to use command line. Hopefully this is not a dump question.
I have the following files in my directory:
L001_R1_001.fastq
L002_R2_001.fastq
L004_R1_001.fastq
L005_R2_001.fastq
L001_R2_001.fastq
L003_R1_001.fastq
L004_R2_001.fastq
L006_R1_001.fastq
L002_R1_001.fastq
L003_R2_001.fastq
L005_R1_001.fastq
L006_R2_001.fastq
You can see in the filenames, it's a mix of R1 and R2 and the numbers after L00 are not sorted.
I want to concatenate files in the order of filename, separately for R1 and R2 files.
If I do it manually, it will look like the following:
# for R1 files
cat L001_R1_001.fastq L002_R1_001.fastq L003_R1_001.fastq L004_R1_001.fastq L005_R1_001.fastq L006_R1_001.fastq > R1.fastq
# for R2 files
cat L001_R2_001.fastq L002_R2_001.fastq L003_R2_001.fastq L004_R2_001.fastq L005_R2_001.fastq L006_R2_001.fastq > R2.fastq
Could you please help me write a script that I can re-use later?
Thank you!
cat `ls -- *_R1_*.fastq | sort` >R1.fastq
cat `ls -- *_R2_*.fastq | sort` >R2.fastq
The | sort is not needed on most systems because ls sorts the files by name.
If the names of the files contain whitespace, then do this first:
IFS='
'
Try using wildcard character *. It will automatically expand file names in alphabetical order.
cat L*_R1_001.fastq > R1.fastq
cat L*_R2_001.fastq > R2.fastq
EDIT:
If above command doesn't give desired sorting, try overriding locale setting using LC_ALL=C as sugested by Fredrik Pihl
LC_ALL=C cat L*_R1_001.fastq > R1.fastq

Partial directory list in linux

If I have a directory containing hundreds of files, using ls, ls-l, or dir gives me a list that's too long for the command terminal screen, so I'm unable to see most of the files in the directory.
I recall there being some argument for ls that allows one to scroll through the list in short increments, but can't seem to find it.
One option is to pipe the output to less or more
ls | less
or
ls | more
Try doing this in a shell :
ls -1 | less
One more way is to redirect the output of ls into a temporary file and then view that file with any editor of your choice - that way you can do searches etc. as well:
ls > res.tmp
vim res.tmp
emacs res.tmp
gedit res.tmp
grep "pattern" res.tmp

Diff-ing files with Linux command

What Linux command allow me to check if all the lines in file A exist in file B? (it's almost like a diff, but not quite). Also file A has uniq lines, as is the case with file B as well.
The comm command compares two sorted files, line by line, and is part of GNU coreutils.
Are you looking for a better diff tool?
https://stackoverflow.com/questions/12625/best-diff-tool
So, what if A has
a
a
b
and b has
a
b
What would you want the output to be(yes or no)?
Use diff command.
Here is a useful vide with complete usage of diff command under 3 min
Click Here
if cat A A B | sort | uniq -c | egrep -e '^[[:space:]]*2[[:space:]]' > /dev/null; then
echo "A has lines that are not in B."
fi
If you do not redirect the output, you will get a list of all the lines that are in A that are not in B (except each line will have a 2 in front if it). This relies on the lines in A being unique, and the lines in B being unique.
If they aren't, and you don't care about counting duplicates, it's relatively simple to transform each file into a list of unique lines using sort and uniq.

Resources