Unix - pipeline ls - la | less C executable giving double total file size vs shell - shell

*Edit - Stephen has answered this question in the comments below *
so basically I have made two seperate child processes(using two seperate methods with their own fork) to execute the command ls -la | less using pipe.
The first one executes ls like this:
execl("/bin/ls", "ls", "-la", NULL);
The second child process executes less like this:
execlp("less", "less", NULL);
And the results come up fine.. apart from one little part:
Results using shell command:
total 15
drwxr-xr-x 2 daniel staff 4 2015-02-27 18:58 .
drwxr-xr-x 15 daniel staff 24 2015-02-27 18:58 ..
-rwxr-xr-x 1 daniel staff 9280 2015-02-27 18:58 pipes
-rw-r--r-- 1 daniel staff 1419 2015-02-27 18:58 pipes.c
Results using my executable:
total 30
drwxr-xr-x 2 daniel staff 4 Feb 27 18:58 .
drwxr-xr-x 15 daniel staff 24 Feb 27 18:58 ..
-rwxr-xr-x 1 daniel staff 9280 Feb 27 18:58 pipes
-rw-r--r-- 1 daniel staff 1419 Feb 27 18:58 pipes.c
Now the date being a different format I don't care about.. but the total size is twice as large with my executable(30 vs 15). Why is this happening?

Make sure that the ls you are running from the shell and the ls that is running in your program are the same program.
Your program is specifying /bin/ls as the program to run; you can find out what is being run when you type the command at the shell prompt by using the shell command which ls (also see type ls).
If these are different it could be due to the POSIX vs. GNU blocksize used in the total size computation.

This answer indicates that POSIX and GNU tools differ in the blocksize they use to compute the total. Try adding h to your command and compare the results (this flag makes the units explicit). My guess is that the execution environment of these processes is disturbing the result. For instance it may not be the same ls that you are running.

Related

Cannot give myself root my permissions

I am trying to give myself root access to all the file in this folder and not have to sudo everything I want to run a command.
The file I am concerned with is pro
When I enter ls -l I get :
drwxr-xr-x+ 12 Guest _guest 384 13 Jan 14:56 Guest
drwxrwxrwt 9 root wheel 288 13 Jan 14:30 Shared
drwxr-xr-x+ 148 Santi staff 4736 1 Apr 17:13 pro
then I enter chmod 775 pro/
It doesnt seem to change the permssions. What can I do to fix this or why is the folder restricting permission even though I appear to be root?
drwxr-xr-x+ ...
the final + means that the file is governed by acl
see
apropos acl : give you the mans to consult
wikipedia
Access Control Lists on Arch wiki

Emacs 25.2 dired mode shows .. above . How do I change this?

On my win-10 machine, dired shows this:
drwxr-xr-x 58 jkb jkb 4096 Nov 5 11:08 ..
drwxr-xr-x 6 jkb jkb 4096 Nov 6 09:34 .
Where the .. entry comes first. How do I modify the installation to change it back to this:
drwxr-xr-x 6 jkb jkb 4096 Nov 6 09:34 .
drwxr-xr-x 58 jkb jkb 4096 Nov 5 11:08 ..
It is not a big issue, except that on my mint 18 machine it shows the traditional order. Uniformity would be good.
Many thanks for pointers and advice.
[See the first comment to this question for the answer]
Formal answer:
I can solve my issue by setting ls-lisp-UCA-like-collation to nil:
'(ls-list-UCA-like-collation nil)
in the customize section of my .emacs file or via the customize menus.
The change has the side-effect of changing the ordering of the cygwin home directory, the ~/.* files come first now.
There may be another option to fix that, if it is important.
Note that this is an issue with dired on emacs, using ls-lisp.

Sort cmd not treating same file whether bash script or terminal

i need to sort some data in order to compare them. I use the command sort.
When it's use in a terminal it works well but when i use it in a script it reduces the file.
size of the dbs :
-rw-r--r-- 1 apache apache 10646 24 juil. 16:43 db1.txt
-rw-r--r-- 1 apache apache 10664 24 juil. 16:43 db2.txt
directly in the terminal
sort db1.txt > db1sorted.txt
sort db2.txt > db2sorted.txt
gives
-rw-r--r-- 1 apache apache 10646 24 juil. 16:06 db1sorted.txt
-rw-r--r-- 1 apache apache 10664 24 juil. 16:06 db2sorted.txt
While with the following script (of course input are db1.txt and db2.txt ):
FILE1=$1
FILE2=$2
sort ${FILE1} > db1sortedd.txt
sort ${FILE2} > db2sortedd.txt
gives
-rw-r--r-- 1 apache apache 10646 24 juil. 17:01 db1sortedd.txt
-rw-r--r-- 1 apache apache 8193 24 juil. 17:01 db2sortedd.txt
I don't understand why the db2sortedd is smaller with the script than with the terminal. Data are disapearing from the letter s.
Thank You in advance for your help
As suggested i tried with smaller files ( 10 lines ) and it works well.
So it's working for small files but not for big one...

Bash - What is the best way to save the value of a quantity after the script has terminated?

I am attempting to write a script that changes my desktop wallpaper to the next picture in my ~\Picture\Wallpapers directory. I have already created a script, change_wallpaper, which can change my desktop background to any given picture.
My plan was to list all the pictures in ~\Picture\Wallpapers as an array, find the index i of the current wallpaper, and apply change_wallpaper to the i+1 element of the array of pictures. My only issue is finding a way to story the index of the current wallpaper. I could create a file which contains this value, but it seems really inelegant to just have a text file with the number 12, for example, saved somewhere in my home directory.
Is there a better way?
I agree with the general consensus of using a file to store the index of the current wallpaper. A slightly more elegant way would be to put a "." (dot) in front of the file name to keep it hidden in the directory where your script is located. Something like:
admin#myHost:/home/admin$ ls -la
total 6200
drwxrwxr-x 4 admin admin 4096 Jul 02 15:41 .
drwxr-xr-x 71 bin bin 4096 Jun 29 12:43 ..
-rw------- 1 admin admin 3939 Jun 29 16:02 .bash_history
-rw-r--r-- 1 admin admin 21 Nov 04 2014 .bashrc
-rwxr-x--x 1 admin admin 259 Oct 13 2014 .profile
-rw------- 1 admin admin 3939 Jun 29 16:02 .wallpaper_index
-rw------- 1 admin admin 3939 Jun 29 16:02 change_wallpaper
There are countless linux and unix applications that do this.

Use rsync to copy only hidden files

I want to back up all the hidden files and directories in my homedir using rsync, but not the non-hidden files and directories.
For example, given this directory listing:
drwxr-xr-x 7 sophie sophie 238 31 Mar 08:45 .
drwxr-xr-x 15 sophie sophie 510 31 Mar 08:14 ..
-rw-r--r-- 1 sophie sophie 4 31 Mar 08:12 .foo
drwxr-xr-x 3 sophie sophie 102 31 Mar 08:45 .hiddendir
drwxr-xr-x 4 sophie sophie 136 31 Mar 08:13 VisibleDirectory
-rw-r--r-- 1 sophie sophie 9 31 Mar 08:13 VisibleFile
I want to back up .foo, .hiddendir, and all the contents of .hiddendir whether they are hidden or not. I don't want to back up VisibleDirectory or VisibleFile.
All the incantations I have come up with back up ".", and therefore all its contents including VisibleFile and VisibleDirectory, and I can't figure out how to exclude it. Please help!
I'm using Mac OS X 10.5.6 (Leopard) and rsync version 2.6.9 protocol version 29.
A common pattern to match the hidden items is .[^.]*
rsync -a ~/.[^.]* /path/to/backup
This copies all files starting with a single dot. Note that it doesn't include files starting with more than one dot.
It's usually ".??*" to make sure you don't copy "." and ".."
(What if you had a file that was just ".a" ?)
Have you tried incarnations like ./.*?
Could you copy the hidden files to a temp directory, back up the temp directory, then remove it?

Resources