Basic Unix refresher inquiry: ls -ld - linux

I know this is really basic, but I cannot find this information
in the ls man page, and need a refresher:
$ ls -ld my.dir
drwxr-xr-x 1 smith users 4096 Oct 29 2011 my.dir
What is the meaning of the number 1 after drwxr-xr-x ?
Does it represent the number of hard links to the direcory my.dir?
I cannot remember. Where can I find this information?
Thanks,
John Goche

I found it on Wikipedia:
duuugggooo (hard link count) owner group size modification_date name
The number is the hard link count.
If you want a more UNIXy solution, type info ls. This gives more detailed information including:
`-l'
`--format=long'
`--format=verbose'
In addition to the name of each file, print the file type, file
mode bits, number of hard links, owner name, group name, size, and
timestamp (*note Formatting file timestamps::), normally the
modification time. Print question marks for information that
cannot be determined.

That is the number of named (hard links) of the file. And I suppose, there is an error here. That must be at least 2 here for a directory.
$ touch file
$ ls -l
total 0
-rw-r--r-- 1 igor igor 0 Jul 15 10:24 file
$ ln file file-link
$ ls -l
total 0
-rw-r--r-- 2 igor igor 0 Jul 15 10:24 file
-rw-r--r-- 2 igor igor 0 Jul 15 10:24 file-link
$ mkdir a
$ ls -l
total 0
drwxr-xr-x 2 igor igor 40 Jul 15 10:24 a
-rw-r--r-- 2 igor igor 0 Jul 15 10:24 file
-rw-r--r-- 2 igor igor 0 Jul 15 10:24 file-link
As you can see, as soon as you make a directory, you get 2 at the column.
When you make subdirectories in a directory, the number increases:
$ mkdir a/b
$ ls -ld a
drwxr-xr-x 3 igor igor 60 Jul 15 10:41 a
As you can see the directory has now three names ('a', '.' in it, and '..' in its subdirectory):
$ ls -id a ; cd a; ls -id .; ls -id b/..
39754633 a
39754633 .
39754633 b/..
All these three names point to the same directory (inode 39754633).

Trying to explain why for directory the initial link count value =2.
Pl. see if this helps.
Any file/directory is indentified by an inode.
Number of Hard Links = Number of references to the inode.
When a directory/file is created, one directory entry (of the
form - {myname, myinodenumber}) is created in the parent directory.
This makes the reference count of the inode for that file/directory =1.
Now when a directory is created apart from this the space for directory is also created which by default should be having two directory entries
one for the directory which is created and another for the
parent directory that is two entries of the form {., myinodenumber}
and {.., myparent'sinodenumber}.
Current directory is referred by "." and the parent is referred by ".." .
So when we create a directory the initial number of Links' value = 1+1=2,
since there are two references to myinodenumber. And the parent's number
of link value is increased by 1.

Related

Change directory using relative path

When I open terminal, my working directory is a/b/c/
and my shell script is placed under a/b/d/e/f/ which gets triggered through Autosys.
I want to change my working directory from a/b/c/ to a/b/d/e/f/ using relative path.
Currently I'm hard coding cd a/b/d/e/f/. I don't want to do it anymore. Could you please let me know how can this be achieved.
cd ../d/e/f/
The trick being the .. which is the "parent directory" of the one you're in
You can see a reference to it (including its permissions) if you use ls -a or more completely
$ ls -lhaF
total 0
drwxr-xr-x 20 user group xxB Jun 15 00:00 ./
drwxr-xr-x 3 user group xxB Jun 15 00:00 ../
-rw-r--r-- 1 user group xxB Jun 15 00:00 e/

rename first part of the filename in linux?

I have lot of files starting with processConfig-. I wanted to rename it to processCfg-. What's the easy way to change the first part of file name to processCfg- in linux?
But I don't want to rename this file processConfig.json since it doesn't match with my prefix.
> ls -lrth
total 467
-rw-r--r-- 1 david staff 9.8K May 26 15:14 processConfig-data-1234.json
-rw-r--r-- 1 david staff 11K May 26 15:14 processConfig-data-8762.json
-rw-r--r-- 1 david staff 4.9K May 26 15:14 processConfig-dataHold-1.json
-rw-r--r-- 1 david staff 6.6K May 26 15:14 processConfig-letter.json
-rw-r--r-- 1 david staff 5.6K May 26 16:44 processConfig-data-90987.json
-rw-r--r-- 1 david staff 284K May 28 18:44 processConfig.json
Like this :
rename -n 's/^processConfig-/processCfg-/' processConfig-*.json
Remove -n switch when the output looks good to rename for real.
man rename
There are other tools with the same name which may or may not be able to do this, so be careful.
The rename command that is part of the util-linux package, won't.
If you run the following command (GNU)
$ file "$(readlink -f "$(type -p rename)")"
and you have a result that contains Perl script, ASCII text executable and not containing ELF, then this seems to be the right tool =)
If not, to make it the default (usually already the case) on Debian and derivative like Ubuntu :
$ sudo apt install rename
$ sudo update-alternatives --set rename /usr/bin/file-rename
If you don't have this command with another distro, search your package manager to install it or do it manually (no deps...)
This tool was originally written by Larry Wall, the Perl's dad.

listing files in UNIX owned by a particular user

How do I list the files owned by a particular user in UNIX ?.
If I use ls - l command in a shared directory ,it lists all the files with the details .This shared directory contains many files created by many users in a group and I am in a situation where I want to see the files created only by a particular user. Is there any listing command to give username as the input.
Refer below example,
command : ls - l
drwxr-xr-x 2 user_1 main 4.0K Feb 12 16:43 proj_1
drwxrws--- 6 user_2 main 20M Feb 18 11:07 proj_2
drwxr-xr-x 3 user_1 main 1.3M Feb 18 00:18 proj_3
drwxrwsr-x 2 user_2 main 8.0K Dec 27 01:23 proj_4
drwxrwsr-x 2 user_3 main 8.1K Dec 27 01:23 proj_5
I am looking for a command to display only the files created by the user_2 with my expected output as below ,
drwxrws--- 6 user_2 main 20M Feb 18 11:07 proj_2
drwxrwsr-x 2 user_2 main 8.0K Dec 27 01:23 proj_4
Kindly let me know if there is a way .
It should be possible to use awk togheter with ls -l
ls -l | awk '$3=="user_2" { print $0 }'
this will print all lines where third field (user) matches "user_2"
You simply can use the findcommand like this:
find . -maxdepth 1 -user some_user -exec ls -lsad {} \;
Why the options are used:
maxdepth we only want to see current directory level
user we only want to see files owned by given user
exec lets do something with the found file
What we want do with the file:
ls -lsad gives you the long list of current file, if it is a directory, don't go into it.

ls to list matches in specified directory

How do I list matched results in a specified directory?
On my Ubuntu server if I list the contents of a directory it correctly lists it. My working directory is /var/crash.
#pwd
/var/crash
# ls -l
-rw-r--r-- 1 bob bob 121876 Aug 8 2015 results.xml
-rw-rw-r-- 1 bob bob 126 Nov 3 2015 start.txt
-rw-rw-r-- 1 bob bob 43 Jul 28 2015 exit.txt
Let's say I want to list all files that contain 'tar'. In this example there should only be one match i.e. start.txt
# ls -l *tar*
-rw-rw-r-- 1 bob bob 126 Nov 3 2015 start.txt
All's good so far. However if I include the directory (/var/crash) it lists all files.
# ls -l *tar* /var/crash
-rw-r--r-- 1 bob bob 121876 Aug 8 2015 results.xml
-rw-rw-r-- 1 bob bob 126 Nov 3 2015 start.txt
-rw-rw-r-- 1 bob bob 43 Jul 28 2015 exit.txt
I'm guessing my syntax is telling ls to list all matches of tar AND everything in /var/crash. What is the correct syntax to list matches in a specified directory?
You need to specify the pattern together with the directory:
ls -l /var/crash/*tar*
Otherwise, with ls -l *tar* /var/crash you are telling ls to act against two parameters: /var/crash and *tar*. In fact, *tar* will be expanded before ls reaches it, so there might be more parameters for ls.

scp file not setting correct owner

Does SCP have a problem setting file permissions or have I misconfiguration my server?
Use case:
There is a file on a server that I want to edit called "importantFile.txt". The file has owner and group of "master":
ls -l importantFile.txt:
-rw-rw-r-- 1 master master 7 Mar 18 08:11 importantFile.txt
I am called "slave" but luckily, I am in group "master" so I can edit the file as I see fit. However, I'm a lazy slave and can't be bothered to edit the file on the server, I'd prefer to edit the file on my local machine and SCP it to the server:
echo "bored slave info" > importantFile.txt
scp importantFile.txt slave#theServerAddress:/pathToFile/importantFile.txt
If I do this, the contents of the file on the server are uploaded fine and the timestamp of the file is updated but the permissions of the file don't change, the file is still owned by "master". This is a problem because if "slave" uploaded bad content, no one would know it was "slave" who caused the problem, "master" would look guilty.
Perhaps I have to set a umask? if so where? I tried .bash_profile without success and haven't found anything on Google about umask in /etc/ssh/sshd_config.
That's nothing special about scp - try logging on to the server as slave, and editing the file using your favourite text editor... You'll find the same behaviour occurs... Writing to a file does not make you the owner of the file.
Example:
as root
#cd /tmp
#mkdir fubar
#chgrp vboxusers fubar
#cd fubar/
#touch testfile
#chgrp vboxusers testfile
#chmod g+w . testfile
#ls -al
total 16
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 0 2009-03-19 10:30 testfile
#echo foo > testfile
#ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 4 2009-03-19 10:30 testfile
as user (in vboxusers group)
>cd /tmp/fubar
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 4 2009-03-19 10:30 testfile
>echo bar >> testfile
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:30 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:29 ..
-rw-rw-r-- 1 root vboxusers 8 2009-03-19 10:31 testfile
>vim testfile
>ls -al
total 20
drwxrwxr-x 2 root vboxusers 4096 2009-03-19 10:31 .
drwxrwxrwt 15 root root 12288 2009-03-19 10:31 ..
-rw-rw-r-- 1 root vboxusers 12 2009-03-19 10:31 testfile
>cat testfile
foo
bar
baz
You have to delete the file to overwrite it. Wether you are able to do that depends on the directory's permissions and ownership. Hijacking ownership of an already existing file is not possible. The write permission you have is only applied on the contents of the file.
It seems you can configure how Emacs deals with this through the backup-by-copying-when-mismatch variable (see the Emacs Manual or type C-h-v backup-by-copying-when-mismatch in Emacs).
I actually filed a bug report about this, because I thought it was a bug in Tramp.
I had misunderstood the way files work, modifying file contents do not change ownership or group.
Why the confusion? EMACS - Whenever I was editing a file I was using Emacs and Emacs does change the owner and group to the current user. It does this because it makes a backup file at save time by moving the "filename" to "filename~" and creating a new file called "filename" - because it's a new file, it has the current users file permissions. I guess this is 1up to VI fans?

Resources