Difference between ls -a and ls -alps - gnu

I don't understand the difference between ls -a and -alps. I can see the result is different but I'm not sure why its display is different. More specifically I don't really know -alps does. it does not show up anywhere when searching for man ls and does not show up on the web.
A follow up question is what do the text prior to the directory names signify? For example:
4 drwxr-xr-x 2 root root 4096 Jan 11 19:18
Thank you!

ls -alps is the same as ls -a -l -p -s
-a: All files are being presented (even if they start if a dot)
-l: The files are being presented in a listing format
-p: A slash (/) is being appended to all directories
-s: The size of each file is being shown

Related

How could I remove all directories except 10 recent with bash?

I have the following folders in my base /var/www/.versions directory:
1435773881 Jul 1 21:04
1435774663 Jul 2 21:17
1435774856 Jul 3 21:20
1435775432 Jul 4 21:56
How could I remove all directories except most 10 recent with bash script?
This should do the trick, I believe?
rm -r $(ls -td /var/www/.versions/*/ | tac | head -n-10)
The idea: list (with ls) only directories ( that's the -d /var/www/.versions/*/) sorted by time with -t (oldest will be shown last).
Then, reverse the output using tac so the oldest directories are on top.
And then show them all except the last 10 lines with head and a negative argument to -n
Please, test with non-vital directories first ;-) You can change the rm -r by echo to see what would be removed.
You could use -rt option in ls for listing in reverse order of time.
rm -r $(ls -trd /var/www/.versions/*/ | head -n -10)
Also, be sure of you put / in the end of /var/www/.versions/*/ and that all directory names do not start with .

Linux sorting "ls -al" output by date

I want to sort the output of the "ls -al" command according to date. I am able to easily do that for one column with command:
$ ls -al | sort -k6 -M -r
But how to do it for both collumn 6 and 7 simultaneously? The command:
$ ls -al | sort -k6 -M -r | sort -k7 -r
prints out results I do not understand.
The final goal would be to see all the files from the most recently modified (or v.v.).
Here is the attached example for the data to be sorted and the command used:
With sort, if you specify -k6, the key starts at field 6 and extends to the end of the line. To truncate it and only use field 6, you should specify -k6,6. To sort on multiple keys, just specify -k multiple times. Also, you need to apply the M modifier only to the month, and the n modifier to the day. So:
ls -al | sort -k 6,6M -k 7,7n -r
Do note Charles' comment about abusing ls though. Its output cannot be reliably parsed. A good demonstration of this is that the image you've posted shows the month/date in columns 4 and 5, so it's not clear why you want to sort on columns 6 and 7.
The final goal would be to see all the files from the most recently modified
ls -t
or (for reverse, most recent at bottom):
ls -tr
The ls man page describes this in more details, and lists other options.
You could try ls -lsa -it -r
sample
enter image description here

How to change ls -a in terminal

I did a lot of customization to bash recently, and I've noticed a small but frustrating problem. When I enter $ ls -a into my home directory it lists all the files and directories one per line rather than the normal two or three per line. What is particularly strange is that this does not happen with $ ls or $ ls -a into any other directory the output is standard.
It only happens with the -a flag when in my home directory. Has anyone else encountered this problem, and have any tips on solving it?
It sounds like you have a particularly long dotfile in your home dir.
ls shows files in columns, but if one filename is exceptionally long, it can only fit one column.
Here's a command that will show the longest filenames in your ls output:
ls -a | awk '{print length($0), $0}' | sort -n

extract a specific word in bash

I have some lines in below forms:
-rw-r--r-- sten/sefan anonymous 8593 2011-12-05 18:28 8M
-rw-r--r-- sten/sefan 8593 2011-12-05 18:28 8M
How can I get the 8593 one-liner?
The lines are retrieved by performing some dry-run of archives, e.g.:
$ tar jtvf zip64support.tar.bz2
-rw-r--r-- stefan.bodewig/Domain Users 16195018 2011-10-14 21:05 100k_Files.zip
-rw-r--r-- stefan.bodewig/Domain Users 14417258 2011-10-14 21:05 100k_Files_7ZIP.zip
or:
$ tar jtvf bla.tar.bz2
-rw-r--r-- tcurdt/tcurdt 610 2007-11-14 18:19 test1.xml
-rw-r--r-- tcurdt/tcurdt 82 2007-11-14 18:19 test2.xml
Specifically to get the number in a line with YYYY-mm-dd after it.
The command you are after to get the filesizes in the current directory is
$ stat -c %s *
You do not want to use bash,awk or cut to do this and your question is a great reason why as in the first line it would be the fourth column and in the second it's the third. Parsing the output of ls is not recommended!
Edit:
Since the column is number is gaurenteed I would use grep with positive lookahead:
$ tar jtvf zip64support.tar.bz2|grep -Po '[0-9]+(?= [0-9]{4}-[0-9]{2}-[0-9]{2})'
16195018
14417258
give this a try:
tar jtvf bla.tar.bz2|awk '$0=$3'
in your question you mentioned
get the number in a line with YYYY-mm-dd after it.
if you really want to do with grep:
tar ... |grep -oP '\d+(?= \d{4}-)'

How to display the output of a Linux command on stdout and also pipe it to another command? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to pipe stdout while keeping it on screen ? (and not to a output file)
For example I want to run the command:
ls -l
Then I have the output to stdout:
drwxr-xr-x 2 user user 4096 Apr 12 12:34 Desktop
-rw-rw-r-- 1 user user 1234 Apr 12 00:00 file
And I want to redirect this output to another command for some further processing (like redirecting to 'head -1' to extract first line). Can I do it in just one line?
Yes, tee will work. Something like:
ls -l | tee | head -1
To append the output to a file:
ls -l | tee -a output.txt
There's a tool called tpipe which allows you to pipe a command to two other commands (like a fork), but it's not installed by default on most machines. Using it, your problem would be solved by:
ls -l | tpipe head -1

Resources