본문 바로가기

Contact 日本語 English

【리눅스】 리눅스 유용 함수 모음

 

리눅스 유용 함수 모음

 

추천글 : 【운영체제】 각론 1강. 리눅스(Linux) 


1. 디렉토리 탐색 [본문]

2. 디렉토리 변경 [본문]

3. 압축 [본문]

4. 프로세스 탐색 [본문]

5. 프로세스 실행 [본문]


 

1. 디렉토리 탐색 [목차]

⑴ 현재 디렉토리 : pwd 

⑵ 주어진 디렉토리에서 파일 개수를 알아내는 코드 

 

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

 

⑶ 주어진 디렉토리에서 .csv 파일 개수를 알아내는 코드 

 

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

 

⑷ 주어진 디렉토리에서 .h5 파일이 아닌 파일들의 절대주소를 출력하는 코드 

 

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

 

⑸ 주어진 디렉토리에서 폴더 개수를 알아내는 코드

 

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

 

⑹ 주어진 디렉토리의 용량을 알아내는 코드

 

du -sh /path/to/directory

 

압축파일을 열어보지 않고 파일들을 보는 코드 

 

unzip -l MyFile.zip

 

⑻ 압축파일을 열어보지 않고 그 안의 파일의 개수를 알아내는 코드 

 

unzip -l MyFile.zip | wc -l

 

어떤 디렉토리 내 모든 파일들의 절대주소들을 나타내는 코드 

 

find /path/to/directory -type f

 

⑽ 대소문자를 무시하고 'ABC'를 포함하는 파일 찾기 : 파일 내용도 탐색

 

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

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

 

대소문자를 무시하고 'ABC'를 포함하는 파일 및 디렉토리 찾기 : 파일 내용도 탐색

 

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).

 

⑿ 정확히 파일명이 'ABC'인 파일들을 디렉토리에서 찾기 

 

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

 

⒀ 특정 디렉토리 내 .loom 파일을 전부 찾아내는 코드

 

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

 

⒁ 특정 디렉토리 내 중복된 파일명 찾기

 

#!/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

 

 

2. 디렉토리 변경 [목차]

⑴ 'ABC'를 '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' '{}' \;

 

⑵ 특정 디렉토리 내 서브 디렉토리 모두 제거 : 즉, 서브 디렉토리로부터 파일들을 모두 꺼내기 

 

#!/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. 압축 [목차]

⑴ 특정 디렉토리에 모든 파일(./*)을 하위 폴더까지 포함하여 test.zip으로 압축

 

zip -r test.zip ./*

 

.merged.png로 끝나는 파일만 모아서 .tar.gz로 압축하기 

 

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

 

 

4. 프로세스 탐색 [목차]

app.py를 사용하는 모든 프로세스에 대한 쿼리 

 

ps -ef | grep app.py

 

 

5. 프로세스 실행 [목차]

 nohup을 이용한 백그라운드 실행

 step 1. .sh 파일 혹은 .py 파일 준비 

 step 2. 터미널 실행

 step 3. 환경 구동 후 실행할 .sh 파일 혹은 .py (예 : app.py) 파일이 있는 경로로 이동

 step 4. 권한 부여 : chmod 755 app.py

 step 5. nohup python app.py \ 

 step 6. 엔터를 한 번 더 쳐야 실행됨

 step 7. (optional) 실행 확인 : ps -ef | grep app.py  nohup.out 확인. "app.py"라는 키워드 인식

 step 8. (optional) 백그라운드 실행 강제 종료 : kill {ProcessID} 

⑵ 반복문 

 

입력: 2023.12.28 14:59