ls to list matches in specified directory - linux

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.

Related

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.

How to separate output for each command generated by a bash file?

Let's say we have a bash script like the one bellow:
echo test
ls -alh
pwd
echo test2
So the file can have any number of commands on it each producing or not its own output.
Then the above file is run like this /bin/bash -xe test.sh which will produce the following output:
+ echo test
test
+ ls -alh
total 32
drwx------+ 6 daniels staff 204B Apr 3 23:33 .
drwxr-xr-x+ 64 daniels staff 2.1K Apr 4 01:53 ..
-rw-r--r--# 1 daniels staff 6.0K Apr 3 23:33 .DS_Store
drwxr-xr-x 5 daniels staff 170B Mar 15 17:03 Todo
-rw-r--r--# 1 daniels staff 282B Apr 3 20:39 test.py
-rw-r--r--# 1 daniels staff 97B Apr 4 01:52 test.sh
+ pwd
/Users/daniels/Desktop
+ echo test2
test2
Is there any way to parse the generated output reliable and figure out how to separate the output based on each command?
For the above example we should be able to separate and extract one group with:
+ echo test
test
another one with
+ ls -alh
total 32
drwx------+ 6 daniels staff 204B Apr 3 23:33 .
drwxr-xr-x+ 64 daniels staff 2.1K Apr 4 01:53 ..
-rw-r--r--# 1 daniels staff 6.0K Apr 3 23:33 .DS_Store
drwxr-xr-x 5 daniels staff 170B Mar 15 17:03 Todo
-rw-r--r--# 1 daniels staff 282B Apr 3 20:39 test.py
-rw-r--r--# 1 daniels staff 97B Apr 4 01:52 test.sh
etc.
I was thinking of parsing the output and look if the line starts with + then take that as the start of one command but then you can easily have something like echo + ok which will make this logic fail.
Another option would've been if we can modify the char that is outputted by /bin/bash -x so that instead of + to output something like https://en.wikipedia.org/wiki/Delimiter#ASCII_delimited_text but looks like + is hardcoded in bash and not configurable.
Any ideas?
+ is not hard-coded, and this is readily described in man bash and under help set for -x:
-x After expanding each simple command, for command,
case command, select command, or arithmetic for
command, display the expanded value of PS4, fol‐
lowed by the command and its expanded arguments or
associated word list.
And here's a further description of PS4, also from man bash:
PS4 The value of this parameter is expanded as with PS1 and
the value is printed before each command bash displays
during an execution trace. The first character of PS4 is
replicated multiple times, as necessary, to indicate mul‐
tiple levels of indirection. The default is ``+ ''.
Here's an example:
$ PS4=$'\nAnd now, a word from '; set -x; date; uptime
And now, a word from date
Mon Apr 3 16:20:35 PDT 2017
And now, a word from uptime
16:20:35 up 65 days, 1:24, 6 users, load average: 1.20, 1.42, 1.37
You can use this to embed special markers or characters as you see fit.

Linux: Finding Newly Added Files

I am trying to obtain a backup of 'newly' added files to a Fedora system. Files can be copied through a Windows Samba share and appear to retain the original created timestamp. However, because it retains this timestamp I am having issues identifying which files were newly added to the system.
Currently, the only way I can think of doing this is to have a master list snapshot of all the files on the system at a specific time. Then when I perform the backup I compare the previous snapshot with a current snapshot. It would detect files that were removed from the system but it seems excessive and I was thinking there must be an easier way to backup newly added files.
Terry
Try using find. Something like this:
find . -ctime -10
That will give you a list of files and directories, starting from within your current directory, that has had its state changed within the last 10 days.
Example:
My Downloads directory looks like this:
kobus#akira:~/Downloads$ ll
total 2025284
drwxr-xr-x 4 kobus kobus 4096 Nov 4 11:25 ./
drwxr-xr-x 41 kobus kobus 4096 Oct 30 09:26 ../
-rw-rw-r-- 1 kobus kobus 8042383 Oct 28 14:08 apache-maven-3.3.3- bin.tar.gz
drwxrwxr-x 2 kobus kobus 4096 Oct 14 09:55 ELKImages/
-rw-rw-r-- 1 kobus kobus 1469054976 Nov 4 11:25 Fedora-Live-Workstation-x86_64-23-10.iso
-rw------- 1 kobus kobus 351004 Sep 21 14:07 GrokConstructor-master.zip
drwxrwxr-x 11 kobus kobus 4096 Jul 11 2014 jboss-eap-6.3/
-rw-rw-r-- 1 kobus kobus 183399393 Oct 19 16:26 jboss-eap-6.3.0-installer.jar
-rw-rw-r-- 1 kobus kobus 158177216 Oct 19 16:26 jboss-eap-6.3.0.zip
-rw-rw-r-- 1 kobus kobus 71680110 Oct 13 13:51 jre-8u60-linux-x64.tar.gz
-rw-r--r-- 1 kobus kobus 4680 Oct 12 12:34 nginx-release-centos-7-0.el7.ngx.noarch.rpm
-rw-r--r-- 1 kobus kobus 3479765 Oct 12 14:22 ngx_openresty-1.9.3.1.tar.gz
-rw------- 1 kobus kobus 16874455 Sep 15 16:49 Oracle_VM_VirtualBox_Extension_Pack-5.0.4-102546.vbox-extpack
-rw-r--r-- 1 kobus kobus 7505310 Oct 6 10:29 sublime_text_3_build_3083_x64.tar.bz2
-rw------- 1 kobus kobus 41467245 Sep 7 10:37 tagspaces-1.12.0-linux64.tar.gz
-rw-rw-r-- 1 kobus kobus 42658300 Nov 4 10:14 tagspaces-2.0.1-linux64.tar.gz
-rw------- 1 kobus kobus 70046668 Sep 15 16:49 VirtualBox-5.0-5.0.4_102546_el7-1.x86_64.rpm
Here's what the find returns:
kobus#akira:~/Downloads$ find . -ctime -10
.
./tagspaces-2.0.1-linux64.tar.gz
./apache-maven-3.3.3-bin.tar.gz
./Fedora-Live-Workstation-x86_64-23-10.iso
kobus#akira:~/Downloads$
Most unices do not have a concept of file creation time. You can't make ls print it because the information is not recorded. If you need creation time, use a version control system: define creation time as the check-in time.
If your unix variant has a creation time, look at its documentation. For example, on Mac OS X (the only example I know of¹), use ls -tU. Windows also stores a creation time, but it's not always exposed to ports of unix utilities, for example Cygwin ls doesn't have an option to show it. The stat utility can show the creation time, called “birth time” in GNU utilities, so under Cygwin you can show files sorted by birth time with stat -c '%W %n' * | sort -k1n.
Note that the ctime (ls -lc) is not the file creation time, it's the inode change time. The inode change time is updated whenever anything about the file changes (contents or metadata) except that the ctime isn't updated when the file is merely read (even if the atime is updated). In particular, the ctime is always more recent than the mtime (file content modification time) unless the mtime has been explicitly set to a date in the future.
"Newly added files, Fedora" : The below examples will show a list with date and time.
Example, all installed packages : $ rpm -qa --last
Example, the latest 100 packages : $ rpm -qa --last | head -100
Example, create a text file : $ rpm -qa --last | head -100 >> last-100-packages.txt

strange behaviour with .git folder

I have clone a git directory as user bob on a remote machine (after having forwarded the local key). When I clone the repo, the repo appears fine and everything shows up including a .git folder.
Now, I want to copy this folder over to another location which happens to be a link to a folder. When I do a cp -r /tmp/tmp.kk3a8xemvr/* /home/staging/myapp, everything gets copied, but not .git folder. When I explicitly call out the .git folder in the cp command, it gets copied. I also noticed the the .gitignore did not get copied either.
Another strange behavior is when I go to /home/staging/myapp, and I do a rm -rf ./*, the .git folder does not get deleted unless I call it out specifically.
How come simple unix commands behave differently for the .git directory? There is nothing special about its permissions. Could it be that the period in front of the file causes some problems?
drwxr-xr-x 8 staging staging 4096 Oct 12 03:42 .git/
My OS is Ubuntu 12.04 LTS.
Linux myapp 3.2.0-54-virtual #82-Ubuntu SMP Tue Sep 10 20:31:18 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
EDIT
Provide more information for Jonathan: I have update the foldernames to match with the commands below:
# cd /tmp/tmp.kk3a8xemvr <- source, everything is good here.
root#myapp1:/tmp/tmp.kk3a8xemvr; ls -la
total 48
drwx------ 9 bob bob 4096 Oct 13 03:08 .
drwxrwxrwt 7 root root 4096 Oct 13 14:55 ..
drwxrwxr-x 3 bob bob 4096 Oct 13 03:08 ansible
drwxrwxr-x 4 bob bob 4096 Oct 13 03:08 backend
drwxrwxr-x 3 bob bob 4096 Oct 13 03:08 clientdb
drwxrwxr-x 15 bob bob 4096 Oct 13 03:08 dapi
drwxrwxr-x 3 bob bob 4096 Oct 13 03:08 docs
drwxrwxr-x 3 bob bob 4096 Oct 13 03:08 fabfile
drwxrwxr-x 8 bob bob 4096 Oct 13 03:08 .git
-rw-rw-r-- 1 bob bob 44 Oct 13 03:08 .gitignore
-rw-rw-r-- 1 bob bob 68 Oct 13 03:08 README.md
-rw-rw-r-- 1 bob bob 450 Oct 13 03:08 requirements.txt
# cd /home/staging; ls
lrwxrwxrwx 1 staging staging 62 Oct 13 03:06 myapp -> /srv/www/staging.myapp.com/public_html/myapp/
# command I use:
sudo cp -r /tmp/tmp.kk3a8xemvr/* /home/staging/myapp
# cd /home/staging/myapp; ls -la; # <- this is the intended destination;
# .git and .gitignore are missing after applying the cp command.
drwxr-xr-x 9 staging staging 4096 Oct 13 14:59 .
drwxr-xr-x 3 staging staging 4096 Oct 13 03:06 ..
drwxr-xr-x 3 staging staging 4096 Oct 13 03:08 ansible
drwxr-xr-x 4 staging staging 4096 Oct 13 03:08 backend
drwxrwxr-x 2 staging staging 4096 Oct 13 14:59 clientdb
drwxr-xr-x 15 staging staging 4096 Oct 13 03:09 dapi
drwxr-xr-x 3 staging staging 4096 Oct 13 03:08 docs
drwxr-xr-x 3 staging staging 4096 Oct 13 03:08 fabfile
-rw-r--r-- 1 staging staging 68 Oct 13 03:08 README.md
-rw-r--r-- 1 staging staging 450 Oct 13 03:08 requirements.txt
If you do:
sudo cp -r /tmp/tmp.kk3a8xemvr/* /home/staging/myapp
the shell expands the * into 'all file or directory names that do not start with .' (thus omitting .git, etc).
If you do:
sudo cp -r /tmp/tmp.kk3a8xemvr /home/staging/myapp
you will get a subdirectory tmp.kk3a8xemvr created in /home/staging/myapp, which is not what you want, I think.
However, if you do:
sudo cp -r /tmp/tmp.kk3a8xemvr/. /home/staging/myapp
you should find you get everything copied where you want it.

Basic Unix refresher inquiry: ls -ld

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.

Resources