In this directory when I run
ls -l
it prints the following output:
-rw------- 1 csundl dcsugrad 0 Dec 5 13:51 file3
drwx------ 2 csundl dcsugrad 4096 Dec 5 13:51 Photos
drwx------ 2 csundl dcsugrad 4096 Dec 5 13:51 Pron
drwx------ 2 csunfi dcsugrad 4096 Dec 5 13:51 Spreadsheets
drwx------ 3 csundl dcsugrad 4096 Dec 5 15:12 Stuff
-rwx------ 1 csundl dcsugrad 149 Dec 5 15:08 untitled.sh
The bolded values are the byte sizes (?).
However when I run:
ls -l | wc -c
The total byte size comes up as 340. Why is this?
Thanks.
ls command displays size of the file
but this command:
ls -l | wc -c
counts number of characters in the output of ls command.
To count total size of files in a directory use du command:
du -hs mydir
Related
I write below statements in my .bashrc
alias ls='ls -l'
$LS = 'ls'
I expected $LS execute ls -l in command line prompt because ls already modified to ls -l. but $LS execute ls only ls.
ls execute ls -l because alias as below:
> ls
total 28
-rw-r--r-- 1 chlee chlee 82 8월 30 22:07 '#test.py#'
drwxr-xr-x 2 chlee chlee 4096 8월 21 04:09 exer
-rw-r--r-- 1 chlee chlee 64 8월 30 21:49 test.py
-rw-r--r-- 1 chlee chlee 49 8월 30 21:47 test.py~
drwxr-xr-x 29 chlee chlee 12288 8월 21 02:51 vsdbg
$LS execute only ls as below:
> $LS
'#test.py#' exer test.py test.py~ vsdbg
How can I get the shell to take into account aliases when variable evaluation?
This should work as you expect:
#!/bin/bash
alias ls="ls -l"
LS () {
ls
}
echo trying lower
ls
echo Trying upper
LS
The function is called LS including parses aliases.
# source LS.sh
trying lower
total 4
-rwxr-xr-x 1 root root 89 Sep 19 21:50 LS.sh
Trying upper
total 4
-rwxr-xr-x 1 root root 89 Sep 19 21:50 LS.sh
Jetchisel was pointing you to this in the comments.
Is there any way to take away the owner's permission to read a file in macOS? I know there's no reason to do this but I have to for school and I can't find an answer anywhere. Removing my write permission works fine but when I try to remove my read permission it automatically give me my read and write permissions back. As you can see in the console when I use chmod -v -v (extra verbose) it shows the correct permissions it should be changed to but then when checking afterwards they havent changed into that...
thijs#Thijss-MacBook-Air-2 week6 % ls -l
total 16
-rw----r-- 1 thijs staff 12 Oct 11 21:10 greeting.txt
-rw-r--r-- 1 thijs staff 0 Oct 11 21:10 hello.txt
-rw------- 1 thijs staff 15 Oct 11 21:11 weather.txt
thijs#Thijss-MacBook-Air-2 week6 % chmod -v -v u-w weather.txt
weather.txt: 0100600 [-rw------- ] -> 0100400 [-r-------- ]
thijs#Thijss-MacBook-Air-2 week6 % ls -l
total 16
-rw----r-- 1 thijs staff 12 Oct 11 21:10 greeting.txt
-rw-r--r-- 1 thijs staff 0 Oct 11 21:10 hello.txt
-r-------- 1 thijs staff 15 Oct 11 21:11 weather.txt
thijs#Thijss-MacBook-Air-2 week6 % chmod -v -v u-r weather.txt
weather.txt: 0100400 [-r-------- ] -> 0100000 [---------- ]
thijs#Thijss-MacBook-Air-2 week6 % ls -l
total 16
-rw----r-- 1 thijs staff 12 Oct 11 21:10 greeting.txt
-rw-r--r-- 1 thijs staff 0 Oct 11 21:10 hello.txt
-rw------- 1 thijs staff 15 Oct 11 21:11 weather.txt
When I execute the ls -l -h command, I get an output as show by the image below.
How can the number of the items in a folder be included in the output?
Update
The current output looks like this
total 41M
-rw-r--r-- 1 root root 41M Dec 20 09:56 completed_projects.bson
-rw-r--r-- 1 root root 213 Dec 20 09:57 completed_projects.metadata.json
drwxrwxr-x 2 adipster adipster 4.0K Jun 16 13:22 contents
-rw-rw-r-- 1 adipster adipster 13 Jun 16 13:20 file.py
drwxrwxr-x 4 adipster adipster 4.0K Jun 16 13:22 folder
drwxrwxr-x 2 adipster adipster 4.0K Jun 16 13:21 items
But I'll like to have another column indicating the number of items in a folder like this
total 41M
-rw-r--r-- 1 root root 41M Dec 20 09:56 completed_projects.bson
-rw-r--r-- 1 root root 213 Dec 20 09:57 completed_projects.metadata.json
drwxrwxr-x 2 adipster adipster 4.0K Jun 16 13:22 contents 235
-rw-rw-r-- 1 adipster adipster 13 Jun 16 13:20 file.py
drwxrwxr-x 4 adipster adipster 4.0K Jun 16 13:22 folder 19
drwxrwxr-x 2 adipster adipster 4.0K Jun 16 13:21 items 5
where the numbers at the extreme right represents the number of items in a folder
You can do something like this:
echo -n "Number of files in folder is: " && ls | wc -l && ls -l
ouptut should be something like this:
umber of files in folder is: 3
Total 279K
-rwxr-xr-x 1 user users 19K Jun 16 00:17 a
-rwxr-xr-x 1 user users 5K Jun 16 00:17 b
-rwxr-xr-x 1 user users 255K Jun 16 00:17 c
You can omit echo statement, just as a note -n is no new line flag.
sed has an option to execute the constructed replacement with /e.
We only count subdirs, looking at the first character
ls -l | sed -r 's/d(.*) ([^ ]*)/printf "d%s %-20s%s\n" "\1" \2 $(ls \2| wc -l)/e'
EDIT: Solution for directories with spaces in their name.
Parsing ls should be avoided. When you try to fix above cmmand for directory names with spaces, you might try
# Don't do this
ls -l | sed -r 's/d(.{,48}) (.*)/printf "d%s %-20s%s\n" "\1" "\2" $(ls "\2"| wc -l)/e'
It is time to write a script. Perhaps with find or something like
#/bin/bash
for i in *; do
printf "%-70s %s\n" "$(/bin/ls -ld "$i")" "$(/bin/ls -d "$i"/* 2>/dev/null| wc -l)"
done
The wc in the subdir will count wrong when filenames have newlines.
ls() { command ls "$#" | tee >(echo "$(wc -l) items"); }
That uses an output process substitution to run the little "echo" script on its stdin while also displaying stdin (thanks to tee). This way, you don't have to run ls twice.
Usual caveat: output will be incorrect when there's a file with a newline in the name.
Let's assume that command1 is processing something and I am interested in BOTH output of command1 as well as how many lines the output actually had.
$ command1 | wc - l
prints number of lines of output of command1, while
$ command1 | nl
prints something like that:
1 ./PaDe014
2 ./PaDe033
3 ./PaDe001
4 ./PaDe013
5 ./PaDe025
6 ./PaDe028
See How to count lines in a document? for more solutions.
However I am interested to generate output like that:
./PaDe014
./PaDe033
./PaDe001
./PaDe013
./PaDe025
./PaDe028
Total number of files generated: 6
I have a vague feeling that it can be achieved with tee and wc, but cannot figure out how exactly.
What is the simplest way to achieve desired output?
I also tried:
command1 | tee >(wc -l)
but there must be a race condition here, as from time to time I receive strange results. Here is the test output:
pdebski#PaDe:~$ (ls -l ; printf "Total: ") | tee -a >(wc -l)
total 56
drwxr-xr-x 7 pdebski pdebski 4096 cze 22 18:50 Data
drwxr-xr-x 2 pdebski pdebski 4096 cze 22 15:14 Desktop
drwxr-xr-x 2 pdebski pdebski 4096 cze 22 14:50 Documents
drwxr-xr-x 2 pdebski pdebski 4096 cze 16 01:09 Downloads
-rw-r--r-- 1 pdebski pdebski 8980 cze 16 01:04 examples.desktop
drwxr-xr-x 4 pdebski pdebski 4096 cze 17 13:44 Music
drwxr-xr-x 2 pdebski pdebski 4096 cze 22 13:14 Pictures
drwxr-xr-x 2 pdebski pdebski 4096 cze 16 01:09 Public
drwxr-xr-x 2 pdebski pdebski 4096 cze 16 01:09 Templates
drwxrwxr-x 2 pdebski pdebski 4096 cze 22 01:21 test
drwxrwxr-x 2 pdebski pdebski 4096 cze 22 01:21 test2
drwxr-xr-x 2 pdebski pdebski 4096 cze 16 01:09 Videos
Total: pdebski#PaDe:~$ 13
A nice illustration for the need of Dijkstra's Semaphores and Mutexes, is not it? (see https://en.wikipedia.org/wiki/Edsger_W._Dijkstra )
Apparently there should be some wait here.
Same results with:
$ (ls -l && printf "Total: ") | tee -a >(wc -l)
I don't know what you mean by "simplest", but for me the simplest solution is:
command1 | awk '1; END { print "\ntotal number of lines:", NR }'
Using process substitution:
command1 | tee >(wc -l)
Or, for appending the Total number of files generated prefix:
(command1 && printf "Total number of files generated: ") | tee >(wc -l)
Reference:
https://unix.stackexchange.com/questions/28503/how-can-i-send-stdout-to-multiple-commands
I have a sequence of image files that look like this:
image-149454.jpg
image-149455.jpg
I have some other images that I want to append to the end of the sequence where it left off but currently they are numbered from 0 (i.e. image-000000 to image-010000).
What's a script I could use to rename those new images starting from a certain number and going on, in this case 149456 and onwards?
I would try something like this. You just have to adjust the offset and add a string in front of the name, just as you need it.
x=1
for i in *.jpg; do
temp=$(printf "%08d.jpg" ${x}) #padding since you seem to want it
mv ${i} ${temp}
let x=x+1
done
Here's an untested solution in Python. This should change all of the files in a directory to have sequentially higher numbers than the other.
To use it enter: script.py dir1 dir2
Assuming the original files have the higher number (149455) are in dir1 and the new files start from 000000 are in dir2:
import os, sys, re
max_image = 0
# check if current (with higher numbers) and other (with lower) directory is given
if len(sys.argv) == 3:
for files in os.listdir(sys.argv[1]): # first dir is current
if files.endswith(".jpg"):
#for all jpgs get the max
m = re.search("\-(\d+)", files)
number = int(m.group(1))
if max_image < number:
max_image = number
for files in os.listdir(sys.argv[2]): # second dir is other
if files.endswith(".jpg"):
#get current start point
m = re.search("\-(\d+)", files)
number = int(m.group(1))
os.rename(files, "image-" + str(number + max_image)+".jpg") # add the max from current folder
Here's a pure bash solution which uses the last image sequence as your starting point (per your request):
#!/bin/bash
last_seq=$(ls image-* | tail -1 | sort -n | cut -c7-) # grab the sequence number
last_seq=${last_seq%.jpg} # remove the trailing .jpg
if [ -z $last_seq ] ; then
echo "Unable to obtain the last image sequence number"
exit 1
fi
for image in image-0*.jpg ; do
[ -f ${image} ] || break # In case no files match image-0*.jpg
let last_seq=last_seq+1
mv -v ${image} image-$(printf "%06d.jpg" ${last_seq})
done
Here's how it worked locally:
before the run:
~/tmp › ls -l
total 80
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:20 image-00000.jpg
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:20 image-01000.jpg
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:05 image-14908.jpg
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:05 image-14909.jpg
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:05 image-14910.jpg
-rw-r--r-- 1 dyoung staff 418 Dec 12 10:24 testh.sh
during the run:
~/tmp › sh ./testh.sh
image-00000.jpg -> image-14911.jpg
image-01000.jpg -> image-14912.jpg
after the run:
~/tmp › ls -l
total 80
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:05 image-14908.jpg
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:05 image-14909.jpg
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:05 image-14910.jpg
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:20 image-14911.jpg
-rw-r--r-- 1 dyoung staff 0 Dec 12 10:20 image-14912.jpg
-rw-r--r-- 1 dyoung staff 429 Dec 12 12:25 testh.sh