Korean, Edit

Collection of Useful Linux Functions

Recommended Article : 【Operating System】 Chapter 1. Linux(Linux)


1. Search for the directory

2. Modify the directory

3. Compress files

4. Query process

5. Execute process



1. Search for the directory

⑴ Current directory : pwd

⑵ Code to find the number of files in a given directory

ls -l /path/to/directory | grep -v '^d' | wc -l

⑶ Code to find the number of .csv files in a given directory

find /path/to/your/directory -type f -name "*.csv" | wc -l

⑷ Code that outputs the absolute addresses of non-.h5 files in a given directory

find /path/to/directory -type f ! -name '*.h5'

⑸ Code to find out the number of folders in a given directory.

ls -l /path/to/directory | grep '^d' | wc -l

⑹ Code to find out the size of a given directory

du -sh /path/to/directory

⑺ Code to view the contents of a zip file without extracting it

unzip -l MyFile.zip

⑻ Code that determines the number of files in a compressed file without extracting it

unzip -l MyFile.zip | wc -l

⑼ Code to display the absolute paths of all files within a certain directory

find /path/to/directory -type f

⑽ Find files containing ‘ABC’, ignoring case differences.


grep -ril "ABC" /path/to/directory

# -r is for recursive search.
# -i ignores case (uppercase/lowercase).
# -l lists only filenames.


⑾ Find files and directories containing ‘ABC’, ignoring case differences


find /path/to/directory -type d -iname "*ABC*" -o -type f -iname "*ABC*"

# -type d searches for directories, -type f for files.
# -iname searches case-insensitively.
# *ABC* is the search pattern (enclosed in asterisks for partial matching).


⑿ Find a file with the exact file name PTPRC.pickle’ in the directory

ls /path/to/folder | grep PTPRC.pickle

⒀ Find files with the exact file name ‘ABC’ in the directory

find /path/to/directory -type f -name "ABC"

⒁ Code to find all .loom files within a specific directory.

find /path/to/directory -maxdepth 1 -name "*.loom" -print

⒂ Find duplicate file names within a particular directory


#!/bin/bash

# Define the target directory
target_directory="/path/to/your/directory"

# Find and list duplicate files based on their MD5 hash
find "$target_directory" -type f -exec md5sum {} + | sort | uniq -d -w 32


⒃ Output time and file information in the order in which files were created in a given directory


ls -ltr --time=creation



2. Modify the directory

⑴ Change ‘ABC’ to ‘ABCD’.


# Renaming Directories and Files (Caution Advised):
## Renaming directories
find /path/to/directory -depth -type d -iname "*ABC*" -execdir rename 's/ABC/ABCD/' '{}' \;

## Renaming files
find /path/to/directory -type f -iname "*ABC*" -execdir rename 's/ABC/ABCD/' '{}' \;

# Replacing Content Inside Files
find /path/to/directory -type f -exec grep -il 'ABC' '{}' \; -exec sed -i 's/ABC/ABCD/gi' '{}' \;


⑵ Remove all subdirectories within a specific directory: that is, extract all files from the subdirectories.


#!/bin/bash

# Base directory containing all target directories
base_directory="/path/to/your/directory"

# Iterate over each directory within the base directory
for target_directory in "$base_directory"/*/; do
    # Find all files and move them with a new name to avoid duplicates
    find "$target_directory" -mindepth 2 -type f -print0 | while IFS= read -r -d $'\0' file; do
        # Construct new filename by replacing '/' with '_' and removing the leading path
        new_name=$(echo "$file" | sed -e "s|$target_directory||" -e 's|/|_|g')
        # Move and rename the file
        mv "$file" "$target_directory$new_name"
    done

    # Remove empty directories
    find "$target_directory" -mindepth 1 -type d -empty -delete
done



3. Compress files

⑴ Compress all files in a specific directory, including subfolders, into test.zip.

zip -r test.zip ./*

⑵ Collecting only files ending with .merged.png and compressing them to .tar.gz

find /path/to/directory -type f -name '*.merged.png' -print0 | tar -czvf merged_png_files.tar.gz --null -T -



4. Query process

⑴ Query for all processes using app.py

ps -ef | grep app.py



5. Execute process

⑴ Running in the background using nohup

Step 1. Prepare a .sh file or a .py file

Step 2. Open the terminal

Step 3. After setting up the environment, navigate to the directory containing the .sh or .py file to be executed (for example: app.py)

Step 4. Grant permission: chmod 755 app.py

Step 5. Execute with nohup: nohup python app.py \

Step 6. Press Enter once more to execute

Step 7. (Optional) To check execution: ps -ef | grep app.py → Check nohup.out. The keyword “app.py” should be recognized

Step 8. (Optional) To forcibly terminate background execution: kill {ProcessID}

⑵ Loops



Input : 2023.12.28 14:59

results matching ""

    No results matching ""