How to suppress the error and display only the final output of du Linux command - tail

Running du command displays following output -
[pz#texualp ~]$ du -s /My-Data/
du: cannot read directory ‘/My-Data/ROMANIEI/smart/store’: Permission denied
du: cannot read directory ‘/My-Data/ROMANIEI/smart/data’: Permission denied
du: cannot read directory ‘/My-Data/ROMANIEI/smart/temp’: Permission denied
du: cannot read directory ‘/My-Data/ROMANIEI/smart/spool’: Permission denied
2900883608 /My-Data/
[pz#texualp ~]$
What is the way to display the last line only (i.e. 2900883608 /My-Data/)?
I have also tried - du -s /My-Data/ | tail -n1, but it did not work.
I would also like to know if there is any way to display only the size ( i.e. 2900883608 ).

As it mention in comments you should redirect the STDERR (where those errors are printed). And this can be done on this way:
du -s /My-Data/ 2>/dev/null
P.S. And if you do not have permission to read particular directory you will not get proper result, you will see the size only of directories where you have permissions.

Related

How to Create a long listing of all the files in this directory to a file called etcFiles.txt (In Linux)

I started to learn Linux. But I dont know how to solve this problem. I want to Create a long listing of all the files in /etc/ directory to a file called etcFiles.txt.When i try to run this terminal says "Permission denied".enter image description here
To long list a file in Linux, you need to use the command
ls -l
It displays the contents on the console. To store it in the file you need to redirect it using redirection operation > to a file like
ls -l directoryPath > outputFile.txt.
Here, to store the result of long listing /etc/ to file you need to use
ls -l /etc/ > etcFiles.txt
In the image linked, to store the contents of the current directory to a file you need to provide the current directory as the argument to ls command. In Unix/ Linux, the current directory is represented by ., so as shown in the screenshot, you are already in /etc/ directory, to store long listing contents of current directory i.e. /etc/ to the file, you need to use
ls -l . > ~/etcFiles.txt
However ls command takes the current directory as default argument . above can be avoided and the following command will also work
ls -l > ~/etcFiles.txt
Linux /Unix by default does not give any user permission to write/ create files in /etc/ directory and requires elevated permission to make any changes in this directory. Since you do not have permission to create a new file in /etc/ directory, either you need to redirect the output to the file in some directory where you have permission like above, we are storing it in the home directory ~ or else you will have to use sudo for superuser permission to create new file in /etc/ itself.
Since we need redirection operator to write file in /etc/, we can't simply run
sudo ls -l > etcFiles.txt
because ls will run with superuser permission and redirection will be done with default user permission. So you need to club in both to run in elevated permission.
To achieve that spawn a new shell with elevated permission using sudo sh and pass the command as a string with -c option as shown below
Solution 1
sudo sh -c 'ls -l . > etcFiles.txt'
Solution 2
You can make use of pipe | by piping the output of ls -l to a command called tee which basically reads the standard input and writes it to both the standard output and one or more files.
Since you need to write to a file inside /etc/ directory, you need to run tee with sudo for elevated permission.
ls -l | sudo tee etcFiles.txt
This will also print the output to the console. To avoid output to the console, redirect output to /dev/null (take it as dustbin sink to throw unwanted outputs) and your final command becomes
ls -l | sudo tee etcFiles.txt > /dev/null

barnyard2 for snort permission denied

I installed barnyard2 for snort, but when i run command below this error appear.
[root#localhost snort]# barnyard2 -c /etc/snort/barnyard2.conf -d /var/log/snort/ -f snort.log -w /etc/snort/bylog.waldo /etc/snort/gen-msg.map /etc/snort/sid-msg.map -C /etc/snort/classification.config
Running in Continuous mode
--== Initializing Barnyard2 ==--
Initializing Input Plugins!
Initializing Output Plugins!
Parsing config file "/etc/snort/barnyard2.conf"
+[ Signature Suppress list ]+
----------------------------
+[No entry in Signature Suppress List]+
----------------------------
+[ Signature Suppress list ]+
Barnyard2 spooler: Event cache size set to [2048]
ERROR: Can not get write access to logging directory "/var/log/barnyard2". (directory doesn't exist or permissions are set incorrectly or it is not a directory at all)
Fatal Error, Quitting..
Barnyard2 exiting
and permission is:
[root#localhost snort]# ls -l /var/log/barnyard2
-rwxrwxrwx. 1 root root 0 Aug 14 16:35 /var/log/barnyard2
in this link this problem was solved but i don't understand how ...
https://forums.freebsd.org/threads/barnyard2-start-service-error.51378/
It looks like directory flag is missing there. The error message says
ERROR: Can not get write access to logging directory "/var/log/barnyard2". (directory doesn't exist or permissions are set incorrectly or it is not a directory at all)
Probably the last case of /var/log/barnyard2 being not a directory at all might apply.
Backup the file and try creating a directory /var/log/barnyard2 with permissions 640 and corresponding ownership.
EDIT: As long as you do not know the contents of /var/log/barnyard2, rename or move the file to some place ( as root 'mv /var/log/barnyard2 /var/log/barnyard2.old'). Restarting barnyard2 now could help, it might create the directory with appropriate permissions by itself. Otherwise as root type 'mkdir /var/log/barnyard2' and then set permissions by typing 'chmod 640 /var/log/barnyard2'. Additionally check the user under which barnyard2 is running by typing 'ps -u | grep "barnyard2"'. Then find the appropriate group to that user by typing 'groups <user>' and then set the ownership of the directory to the corresponding user by typing 'chown <user>:<group> /var/log/barnyard2'.
'/var/log/barnyard2' should be the log directory. In your case it is a file. So, delete the file and create a directory instead. Here are the steps. Enter the commands as a root user.
rm /var/log/barnyard2
mkdir /var/log/barnyard2

Permission Denied to write /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger

I am trying to run the following command on my Ubuntu machine
root#manav-R761-c:/# ls -la /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
ls: cannot access /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger: No such file or directory
root#manav-R761-c:/#
root#manav-R761-c:/# echo 'hash:stacktrace:bytes_req,bytes_alloc' > /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger
bash: /sys/kernel/debug/tracing/events/kmem/kmalloc/trigger: Permission denied
I am logged in as root, then why I am not able to create trigger file and write to it?

issue with `cp -dR `

I have a directory structure as:/apps/amr. I want to take a backup of amr as it is(i.e without de-referencing links).Now the /apps directory has a different user and group that it belongs to( and i don't have access to that). I am logged in as a different user and group. So obviously in /apps directory executing:
mkdir amr_backup
cp -dR amr amr_backup
doesn't work as permission denied.
So,I am trying to do the same in /apps/amr directory as amr directory has the same user and group i logged in as. But when executing:
mkdir amr_backup
cp -dR * amr_backup
It shows this error:
cp: cannot copy a directory, `amr_backup`, into itself, `amr_backup/amr_backup'
which is obvious because the amr_backup directory itself is within /apps/amr.
And also I can see not everything is backed up properly:
du -sh /apps/amr
8.6G /apps/amr
du -sh /apps/amr/amr_backup
4.3G /apps/amr/amr_backup
So kindly give me a resolution around.
I say, keep it simple...
mkdir .tmp
cp -dR * .tmp/.
mv .tmp amr_backup
The glob expansion of * will not include anything beginning with . so the hidden directory will not itself be a source directory.
You can exclude amr_backup from the copy command by using !(amr_backup) instead of the * wildcard.
cp -dR !(amr_backup) amr_backup/
The file sizes look ok to me. amr is about twice the size of amr_backup because it contains amr_backup.

Permission denied for root shrc

Whenever I open the terminal on my Centos5.1, I always get this error
/root/.cshrc Permission denied
and then I can't use networking commands (ip,ifconfig,...) because they are reported as unknown commands.
Verify that you have permissions to read .cshrc To do that issue:
ls -l /root/.cshrc
If the output begins with to dashes it means that you don't. To give yourself read permission to this file issue:
chmod +r /root/.cshrc
Now if you run ls -l /root/.cshrc the output should start with -r.

Resources