Search + https://www.tecmint.com/35-practical-examples-of-linux-find-command/

1. Search for content within files using regular grep

find ./ -type f -exec grep -Hn "YourContent" {} \;

2. Search for content using the xargs command

find ./ -type f |xargs grep -Hn "abc"

General notes

  • In most situations/directories you won’t need the parameter “-type f”
  • For case insensitive search just apply the parameter -i. Examples:
    find ./ -type f -exec grep -Hni "YourContent" {} \;
    find ./ -type f |xargs grep -Hni "abc"

 

 

Commands:

  1. pwd - determine/locate current folder
  2. cd .. - return to upper string of hierarchy
  3. find . -type d | xargs chmod 755    // make all directories 755 everywhere under Drupal
    find . -type f | xargs chmod 644    // make all files 644 everywhere under Drupal

    find options starting/path expression

        The options attribute will control the behavior and optimization method of the find process.
        The starting/path attribute will define the top level directory where find begins filtering.
        The expression attribute controls the tests that search the directory hierarchy to produce output.

    Consider the following example command:
    find -O3 -L /home/u964450658/public_html -name "*.php"

    This command enables the maximum optimization level (-O3) and allows find to follow symbolic links (-L). find searches the entire directory tree beneath /home/u964450658/public_html for files that end with .php.

  4. chmod 777 files                     // assuming the uploaded files are in this subdirectory
    chdir files
    find . -type d | xargs chmod 777    // some modules may have subdirectories for images etc
    find . -type f | xargs chmod 666    // files now owned by you - modules may need to overwrite them

  5. Удаление строки, где встречается вредный код, содержащий числовую ошибку
    for f in *.php; do sed -i '/4[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/c <?php' $f; done
    sed -i '/4[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]/c <?php' $(find -name "*.php" -type f)

  6. Поиск файла: locate -i filename

  7. ls

    Lists the folder and file names in the current working directory. You can narrow the list down to files matching a specific name by passing the name as a parameter. This comes with a variety of flags; a few key ones are -l to list the long format that displays the file permissions, and -a to list all files including hidden files.

  8. cd

    Change Directory to the path specified, for example cd projects. There are a few really helpful arguments to aid this:

        . refers to the current directory ./projects
        .. can be used to move up one folder use cd .., and can be combined to move up multiple levels ../../my_folder
        /is the root of your system to reach core folders, such as system, users, etc
        ~ is the home directory, usually the path /users/username. Move back to folders referenced relative to this path by including it at the start of your path, for example ~/projects . If you need to access your shell configuration, you can easily find this with ~/.bashrc or ~/.zshrc — then add all sorts of beneficial aliases, configurations, commands and paths.
  9. mkdir

    Make directories with this command mkdir my_folder . Not only can it make the folder specified, but also its parents if they do not exist already using the -p option. The command mkdir -p first_folder/next_folder/my_folder would also create the first and next folder. There is an option to set the mode, or permissions, via the -m flag, and these can be changed later with the chmod command (see below for more on mode and permissions).

  10. touch

    Touches the file to update the access and or modification date of a file or directory without opening, saving or closing the file. But one of the most common uses is to create an empty file touch my_file.

  11. cat

    Concatenate and print files to stdout cat my_file. You can pass one or more file names to this command, and even number the lines using the -n to number the lines. A close cousin of this is vi to launch a terminal-based text editor.

  12. mv

    Moves files and folders. The first argument is the file you want to move, and the second is the location to move it to. Use the flags -f to force move them and -i to prompt confirmation before overwriting files.

  13. cp

    Copies files and folders cp my_file ./projects . The flag -r recursively copies subfolders and files.

  14. rm

    Removes files and folders rm my_folder . Using -r will again recursively delete subfolders, -f force deletes, and -rf for a recursive force delete. If you want to remove all folders and files in the current directory the command is rm -rf ./* , if you leave out the dot then it would reference the root directory!
    There are ways to stop accidental deletes. If you use the -i flag (for interactive) you can specify the computer to prompt confirmation before delete.

  15. chmod

    Change mode so you can set permissions for read, write and execute for the user, members of your group and others. This uses binary values as an argument to set these. There are many common chmod permissions, a few key ones are:

        777 — anyone can read, write and execute chmod 777 my_file
        755 — for files that should be readable and executable by others, but only changeable by the issuing user
        700 — only the user can do anything to the file
  16. man

    Manuals for a command can be shown with this instruction. Below is some of the output from running man ls, it also displays all the options available for running the command.

  17. sed -i -r 's/^(billy|tom)@.*example\.org/\1@example.com/' ~/roster.txt

    This command begins with the invocation (sed) followed by the -i option. -i allows sed to perform the modification “in place” on the file specified. The -r option forces sed to use an extended regular syntax. The next argument enclosed in single quotes (e.g. ') specifies the “substitution” or search and replace function. The final term of a sed command specifies the file object that the substitution function will be applied to.

    sed can also be used to modify streams of text rather than files, so often a command might take the form of:

    cat ~/input-file | sed -r 's/^(billy|tom)@.*example\.org/\1@example.com/' > ~/output-file

    In this case, the contents of the stream of data which is created by running the cat command on the ~/input-file is filtered through the sed operation. The result is written to the ~/output-file. Generally, the left-hand side of the pipe would contain some other form of input, but you do not need to cat files into sed as the above command is equivalent to the following:

    sed -r 's/^(billy|tom)@.*example\.org/\1@example.com/' ~/input-file > ~/output-file

    Unless otherwise directed, sed will output the transformed text to standard output.
  18. Counting Files in the Current Directory = ls -l | grep -v ^d | wc -l

    To determine how many files there are in the current directory, put in ls -1 | wc -l. This uses wc to do a count of the number of lines (-l) in the output of ls -1. It doesn't count dotfiles. Please note that ls -l (that's an "L" rather than a "1" as in the previous examples) which I used in previous versions of this HOWTO will actually give you a file count one greater than the actual count. Thanks to Kam Nejad for this point.

    If you want to count only files and NOT include symbolic links (just an example of what else you could do), you could use ls -l | grep -v ^l | wc -l (that's an "L" not a "1" this time, we want a "long" listing here). grep checks for any line beginning with "l" (indicating a link), and discards that line (-v).

Sed Substitutions

The basic 's///' form provides the core of sed functionality in common use. The s indicates that the script will perform a substitution. The next character introduces a character to separate the “find” and “replace” strings and to terminate the substitution script. By convention the separation characters are / characters, but in cases where you are searching for data that contains / characters, it is possible to use another separating character. Thus, the following two strings are functionally identical:

's/look for \/ characters/I found several \//'
's;look for / characters;I found several /;'

Sed uses regular expressions in the “search” part of the substitution syntax. Most characters in regular expressions match with input data literally; however, there are some sequences that carry special significance. If you want to match these characters literally you can use the escape character \ to transform the next character into a literal match. Consider the following characters with special significance in sed substitution scripts:

    The . symbol matches any character.
    The * symbol causes the character immediately proceeding this character to successfully match to zero or more instances of that character in the data set.
    The + symbol causes the character immediately proceeding this character to successfully match to one or more instances of that character in the data set.
    Square brackets ([]) enclose a set of characters that match against any member of the set of specified characters. When prefaced with a caret, as in [^abc], it matches none of the characters specified in the set.
    Parenthetical characters (left ( or right )) both allow you to write more complex expressions and also create “captures” that allow you to use sequences from the matched text in the replacement string. Captured sequences are available in the order that they were captured with \[number] where [number] corresponds to the number of the capture.
    The ^ character matches the beginning of a line.
    The $ character matches the end of a line.
    The \ character, as previously stated, escapes the following character for literal matching if it caries additional meaning.
    The | character provides an “OR” operator, so the sequence ^(www|ftp)\. would match a line that began with the characters www or ftp.

While these characters provide the foundation of writing matching patterns, there are other significant characters and powerful matching abstractions. Documenting the full capabilities of the sed regular expression syntax is beyond the scope of this guide; however you can learn more about sed commands using the info sed command.
Finding and Replacing Strings within files Using Sed

In some cases, the “in place” substitution with the -i argument provides the desired behavior. However, if you want to test a sed operation, or provide a “safety net”, consider the following command:

sed -r -i.bak 's/example/example/g' ~/roster.txt

In this case, the existing file is copied to ~/roster.txt.bak and the replacements are made automatically to ~/roster.txt. If you want to reverse the changes, issue a command similar to mv ~/roster.txt.bak ~/roster.txt.

The g option appended to the substitution statement sets a “global” mode that forces sed to replace multiple instances of the match on the same line.

Deleting Lines From Files Using Sed

Consider the following expression:

sed -i '56d' ~/.ssh/known_hosts

In this command, the 56th line of the ssh “known hosts” file will be deleted. Commands in this form are useful for deleting a host key for a host that has changed from the known_hosts file, as is the case after redeploying a system or moving an IP or domain to a new host.


How do I run .sh files in Linux?

The procedure to run .sh files on Linux is as follows:

    Set execute permission on your script:
    chmod +x script-name-here.sh
    To run your script, enter:
    ./script-name-here.sh
    OR
    sh script-name-here.sh
    OR
    bash script-name-here.sh

 


fix-permissions.sh

#!/bin/bash

path=${1%/}
user=${2}
group="www-data"
#group="psaserv" # uncomment for Plesk based setup
help="\nHelp: This script is used to fix permissions of a drupal installation\nyou need to provide the following arguments:\n\t 1) Path to your drupal installation\n\t 2) Username of the user that you want to give files/directories ownership\nNote: \"www-data\" (apache default) is assumed as the group the server is belonging to, if this is different you need to modify it manually by editing this script\n\nUsage: (sudo) bash ${0##*/} drupal_path user_name\n"

if [ -z "${path}" ] || [ ! -d "${path}/sites" ] || [ ! -f "${path}/modules/system/system.module" ]; then
echo "Please provide a valid drupal path"
echo -e $help
exit
fi

if [ -z "${user}" ] || [ "`id -un ${user} 2> /dev/null`" != "${user}" ]; then
echo "Please provide a valid user"
echo -e $help
exit
fi

cd $path;
echo -e "Changing ownership of all contents of \"${path}\" :\n user => \"${user}\" \t group => \"${group}\"\n"
chown -R ${user}:${group} .
echo "Changing permissions of all directories inside \"${path}\" to \"750\"..."
find . -type d -exec chmod u=rwx,g=rx,o= {} \;
echo -e "Changing permissions of all files inside \"${path}\" to \"640\"...\n"
find . -type f -exec chmod u=rw,g=r,o= {} \;

cd $path/sites;

echo "Changing permissions of \"files\" directories in \"${path}/sites\" to \"770\"..."
find . -type d -name files -exec chmod ug=rwx,o= '{}' \;
echo "Changing permissions of all files inside all \"files\" directories in \"${path}/sites\" to \"660\"..."
find . -name files -type d -exec find '{}' -type f \; | while read FILE; do chmod ug=rw,o= "$FILE"; done
echo "Changing permissions of all directories inside all \"files\" directories in \"${path}/sites\" to \"770\"..."
find . -name files -type d -exec find '{}' -type d \; | while read DIR; do chmod ug=rwx,o= "$DIR"; done
echo "Applying group inheritance"
sudo chmod -R g+s $path;