GNU/Linux


Get the total size of a directory or file

du -sh someName

*INFO: someName can be replaced by any folder or file name or even a regular expression (for example to get the size of all txt files you need to invoke du -sh *.txt)
By using the parameter -s (which stands for summarize) you will get back only the total size in bytes.
By using the parameter -h (which stands for human readable format), for any size output you will get it back in Kilobytes, Megabytes etc.


Find all files that contain a string (Get filenames) 5

Assuming you have a lot of files and you need to find all files that contain a certain string value, you can check them all automatically using the following command.

Method 1 – Using find -exec

Using the exec flag of find, you can process all files even those that their filenames contain single or double quotes or other special characters.
The following command will return a list, the list of files that contain the value ‘string’.

find . -type f -exec grep 'string' -s -l '{}' \;

The above command breaks down as follows:

  • find . -type f Find all files in current directory.
  • -exec For each match execute the following.
  • grep 'string' '{}' Search the matched file '{}' if it contains the value ‘string’.
  • -s Suppress error messages about nonexistent or unreadable files.
  • -l (lambda lower case) or --files-with-matches Suppress normal output, instead print the name of each input file from which output would normally have been printed. The scanning will stop on the first match.

To exclude binary files from matching, add the -I parameter on grep.

Method 2 – Using xargs

This method will not work for filenames that contain single quotes as it will confuse xargs.

find . -type f | xargs grep 'string' -s -l;

To exclude binary files from matching, add the -I parameter on grep.

Note: WordPress might change/replace the apostrophe to another ASCII apostrophe punctuation symbol! Make sure you use the straight apostrophe symbol or else you will not get the result expected since the behavior of grep will change. See Apostrophe on WikiPedia Here.


Fedora and CentOS GNU/Linux: Add an existing user to the Sudoers list

So, you are a system administrator on a Fedora or a CentOS GNU/Linux machine and a user requests that you upgrade their account to allow the execution of privileged commands using sudo.

Warning

Be very careful to which users you give this right!
Being in the Sudoers list allows particular users to run various commands as the root user, without needing the root password.
Assuming that the user has a valid reason for you to add them to the Sudoers list, proceed with the commands below:

Using sudo

If you are using an account that is already in the Sudoers list and you want to allow the account useraccount to use sudo, execute the following

sudo chmod +w /etc/sudoers
sudo echo 'useraccount ALL=(ALL) ALL ' >> /etc/sudoers
sudo chmod -w /etc/sudoers

Using the root account

If you are using the root user account and you want to allow the account useraccount to use sudo, execute the following

chmod +w /etc/sudoers
echo 'useraccount ALL=(ALL) ALL ' >> /etc/sudoers
chmod -w /etc/sudoers

Notes

The /etc/sudoers file must have very limited access rights for it to be valid.

The system expects that:

  • it will be owned by the root user
  • it will belong to the group root
  • it has only that read access right
  • the read access right belongs only to the owner and to the group

For this reason we first use chmod +w to enable the right access on the file, then we append at the end of the file our configuration using echo >> and finally we remove the write access using chmod -w.

In case you are wondering how the file should be, using ls -l it should appear as follows:

ls -l /etc/sudoers
-r--r-----. 1 root root 3762 Oct 19 13:21 /etc/sudoers

If for some reason your file does not have these access rights, you can repair the file access right of your /etc/sudoers file using

sudo chmod 440 /etc/sudoers

Bonus

No password

Using the above method, it will prompt the user to enter their account password when they first want to use a sudo command after some time of inactivity.

In case you want the user to execute sudo without using a password at all (which is dangerous and definitely not recommended) use the following code

chmod +w /etc/sudoers
echo 'useraccount ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers
chmod -w /etc/sudoers

The NOPASSWD directive in the echo command will instruct the system to not ask for a password when sudo is needed.

A valid occasion for you to allow this would be to allow an automated script to perform some tasks that require elevated privileges without the need to have the password hardcoded in the script not having to get the user involved each time in the process.

Adding a whole group to the sudoers list

Assuming you want enable all users of a specific group to execute sudo commands

Using sudo

If you are using an account that is already in the Sudoers list and you want to allow all the users of the user group usergroup to use sudo, execute the following

sudo chmod +w /etc/sudoers
sudo echo '%usergroup ALL=(ALL) ALL ' >> /etc/sudoers
sudo chmod -w /etc/sudoers

Same thing without a password

sudo chmod +w /etc/sudoers
sudo echo '%usergroup ALL=(ALL) NOPASSWD: ALL ' >> /etc/sudoers
sudo chmod -w /etc/sudoers

Using the root account

If you are using the root user account and you want to allow all the users of the user group usergroup to use sudo, execute the following

chmod +w /etc/sudoers
echo '%usergroup ALL=(ALL) ALL ' >> /etc/sudoers
chmod -w /etc/sudoers

Same thing without a password

chmod +w /etc/sudoers
echo '%usergroup ALL=(ALL) NOPASSWD: ALL ' >> /etc/sudoers
chmod -w /etc/sudoers

Connect with ssh wihout using a password / without login procedure / with passwordless login / with no authentication

From your machine issue the following commands in order to let the server know of your information and allow you to login without going through the interactive authentication phase.

ssh-keygen -t rsa
ssh-copy-id -i ~/.ssh/id_rsa.pub useraccount@boxname

*NOTE: This method does not allow to connect the other way around without credentials. You need to repeat this from the other machine as well to make it two way.