AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-363235

Muhammad Ikhwan Perwira's questions

Martin Hope
Muhammad Ikhwan Perwira
Asked: 2023-07-18 18:46:49 +0800 CST

直接切片字节来分割大文件是否安全?

  • 7

就我而言,大文件是 tar.gz,myBigFile.tar.gz大小为 52GB,我将其分割为大小为 2GB 的块,因此我有 27 个部分文件。

这是我从头开始编写的代码:

from time import sleep
from glob import glob
import filecmp
import os

CHUNK_SIZE = 2097152000  # bytes
# CHUNK_SIZE = 1000000  # bytes
# CHUNK_SIZE = 2  # bytes

ORIGINAL_FILE_DIR = './data/original'
SPLITTED_FILE_DIR = './data/splitted'
JOINED_FILE_DIR = './data/joined'


def get_original_filepath(filename):
  return f'{ORIGINAL_FILE_DIR}/{filename}'


def get_splitted_filepath(filename, overwrite=False):
  partspath = f'{SPLITTED_FILE_DIR}/{filename}.parts'
  if overwrite:
    try:
      os.rmdir(partspath)
    except Exception as e:
      print(e)
    try:
      os.mkdir(partspath)
    except Exception as e:
      print(e)
  return partspath


def get_joined_filepath(filename):
  return f'{JOINED_FILE_DIR}/{filename}'


def get_part_extension(part, pad_num=8):
  if isinstance(part, int):
    return f'{part:0{pad_num}d}.part'
  elif isinstance(part, str):
    return f'{part}.part'
  else:
    raise Exception('Unknown typeof <part>', type(part))


def get_part_filename(filename, part, pad_num=8):
  part_extension = get_part_extension(part, pad_num)
  return f'{filename}.{part_extension}'


def get_file_size(filepath):
  return os.path.getsize(filepath)


def get_number_of_chunks(total_size, chunk_size):
  return total_size // chunk_size + (total_size % chunk_size > 0)


def is_directory_empty(directory_path):
  try:
    # Get the list of files and directories in the specified path
    files = os.listdir(directory_path)

    # Check if there are any files in the list
    if len(files) == 0:
      return True
    else:
      return False
  except:
    # Handle the case when the directory does not exist
    return True


def split_file(filename, chunk_size=CHUNK_SIZE):
  original_path = get_original_filepath(filename)
  if get_file_size(original_path) == 0:
    print(Exception('E: Original file not found!'))
  splitted_path = get_splitted_filepath(filename, overwrite=True)
  with open(original_path, 'rb') as readfile:
    number_of_chunks = get_number_of_chunks(get_file_size(original_path),
                                            chunk_size)
    for part in range(number_of_chunks):
      chunk = readfile.read(chunk_size)
      part_filename = get_part_filename(filename, part,
                                        len(str(number_of_chunks)))
      with open(f'{splitted_path}/{part_filename}', 'wb') as writefile:
        writefile.write(chunk)


def join_file(filename):
  splitted_path = get_splitted_filepath(filename)
  joined_path = get_joined_filepath(filename)
  if is_directory_empty(splitted_path):
    print(Exception('E: Splitted file not found!'))
  part = '*'  # wilcard
  part_filename = get_part_filename(filename, part)
  partfiles = [
      os.path.normpath(fn) for fn in glob(f'{splitted_path}/{part_filename}')
  ]
  with open(joined_path, 'ab') as appendfile:
    for partfile in partfiles:
      with open(partfile, 'rb') as readfile:
        appendfile.write(readfile.read())


def compare_file(filename):
  # Specify the paths of the two files
  file1_path = get_original_filepath(filename)
  file2_path = get_joined_filepath(filename)

  return f'{filename} is identical.' if filecmp.cmp(
      file1_path, file2_path) else f'{filename} is not identical.'


filename = 'myBigFile.tar.gz'

split_file(filename)
join_file(filename)
print(compare_file(filename))

所以 splitted_pa​​th 看起来像这样:

./data/myBigFile.tar.gz.parts/myBigFile.tar.gz.00.part
./data/myBigFile.tar.gz.parts/myBigFile.tar.gz.01.part
...
./data/myBigFile.tar.gz.parts/myBigFile.tar.gz.25.part

我知道我可以使用 Unix 实用程序,例如 tar、zip 或其他归档程序。

我也在小 CHUNK_SIZE 的小文件中测试了它,它加入文件没有任何问题。

files
  • 1 个回答
  • 37 Views
Martin Hope
Muhammad Ikhwan Perwira
Asked: 2022-06-14 05:37:29 +0800 CST

从 CLI 与 GUI 交互(无头 GUI)

  • 1

我有linux服务器。

我希望有可以由 CLI 为我的服务器控制的无头 GUI。

我确实知道可以使用 XRDP 显示 GUI。但我希望我可以使用 SSH 或 CLI 来控制它。当我使用 XRDP 时它工作正常。

我安装了 OpenBox(一个 Windows 管理器)。我希望我可以与 CLI 交互 GUI,或者也许有可以处理它的 Python 库。

mouseclick(2,3) # mouse click area at coordinate (2,3)

screenshot("./current_screen.png") # saving screenshot of current screen in specified path.

库可以处理的另一个功能。

我找到了类似的库,它是pyautogui.

但是pyautogui如果有现有的 GUI 就可以工作。Display Not Found如果我在 CLI 中运行它,我的意思是 python 脚本错误。

# t.py
import pyautogui
print(pyautogui.size())

它给了我错误:

root@server-kentang:~/py# python3 t.py
Traceback (most recent call last):                           File "/usr/local/lib/python3.8/dist-packages/Xlib/support/unix_connect.py", line 76, in get_socket
    s.connect('/tmp/.X11-unix/X%d' % dno)                  FileNotFoundError: [Errno 2] No such file or directory

During handling of the above exception, another exception occurred:

Traceback (most recent call last):                           File "t.py", line 1, in <module>
    import pyautogui
  File "/usr/local/lib/python3.8/dist-packages/pyautogui/__init__.py", line 249, in <module>
    import mouseinfo
  File "/usr/local/lib/python3.8/dist-packages/mouseinfo/__init__.py", line 223, in <module>
    _display = Display(os.environ['DISPLAY'])
  File "/usr/local/lib/python3.8/dist-packages/Xlib/display.py", line 80, in __init__
    self.display = _BaseDisplay(display)
  File "/usr/local/lib/python3.8/dist-packages/Xlib/display.py", line 62, in __init__
    display.Display.__init__(*(self, ) + args, **keys)
  File "/usr/local/lib/python3.8/dist-packages/Xlib/protocol/display.py", line 58, in __init__
    self.socket = connect.get_socket(name, host, displayno)
  File "/usr/local/lib/python3.8/dist-packages/Xlib/support/connect.py", line 76, in get_socket
    return mod.get_socket(dname, host, dno)
  File "/usr/local/lib/python3.8/dist-packages/Xlib/support/unix_connect.py", line 78, in get_socket
    raise error.DisplayConnectionError(dname, str(val))
Xlib.error.DisplayConnectionError: Can't connect to display ":0": [Errno 2] No such file or directory
command-line python
  • 1 个回答
  • 215 Views
Martin Hope
Muhammad Ikhwan Perwira
Asked: 2022-05-23 16:26:15 +0800 CST

用 tar 备份整个 linux 系统到系统本身挂载的分区

  • 2

我所有的操作系​​统都安装在一个分区上,它是/dev/vda1.

如果我用这个命令备份整个 Linux 目录有问题吗

root@myOS:/# tar -cJpf /mnt/bax/myOS.tar.xz .

基本上我备份整个系统并将其保存在我的第二个分区上。

(第二个分区)/mnt/bax安装在哪里。/dev/vda2

恐怕会发生递归备份,我的意思是既然/dev/vda2挂载了,那是否意味着会发生递归备份?

backup tar
  • 2 个回答
  • 623 Views
Martin Hope
Muhammad Ikhwan Perwira
Asked: 2022-05-23 01:48:35 +0800 CST

自动检测网络接口卡,network init.d

  • 0

我已经根据本书构建了我的 LFS linux ,它工作正常,网络接口卡也在工作。即使它使用dhcpcd服务自动配置IP。

根据那本书的第9章,有一个文件叫做

/etc/sysconfig/ifconfig.eth0 问题是,如果我正在使用wlan0,我是否需要手动修改配置文件或将 eth0 重命名为 wlan0 每次更改网络接口卡,我希望它会自动检测到

这里是由 LFS 书籍生成的初始化网络脚本,/etc/init.d/network与/etc/sysconfig/ifconfig.*

### BEGIN INIT INFO
# Provides:            $network
# Required-Start:      $local_fs localnet swap
# Should-Start:        $syslog firewalld iptables nftables
# Required-Stop:       $local_fs localnet swap
# Should-Stop:         $syslog firewalld iptables nftables
# Default-Start:       3 4 5
# Default-Stop:        0 1 2 6
# Short-Description:   Starts and configures network interfaces.
# Description:         Starts and configures network interfaces.
# X-LFS-Provided-By:   LFS
### END INIT INFO

case "${1}" in
   start)
      # Start all network interfaces
      for file in /etc/sysconfig/ifconfig.*
      do
         interface=${file##*/ifconfig.}

         # Skip if $file is * (because nothing was found)
         if [ "${interface}" = "*" ]; then continue; fi

         /sbin/ifup ${interface}
      done
      ;;

   stop)
      # Unmount any network mounted file systems
       umount --all --force --types nfs,cifs,nfs4

      # Reverse list
      net_files=""
      for file in  /etc/sysconfig/ifconfig.*
      do
         net_files="${file} ${net_files}"
      done

      # Stop all network interfaces
      for file in ${net_files}
      do
         interface=${file##*/ifconfig.}

         # Skip if $file is * (because nothing was found)
         if [ "${interface}" = "*" ]; then continue; fi

         # See if interface exists
         if [ ! -e /sys/class/net/$interface ]; then continue; fi

         # Is interface UP?
         ip link show $interface 2>/dev/null | grep -q "state UP"
         if [ $? -ne 0 ];  then continue; fi

         /sbin/ifdown ${interface}
      done
      ;;

   restart)
      ${0} stop
      sleep 1
      ${0} start
      ;;

   *)
      echo "Usage: ${0} {start|stop|restart}"
      exit 1
      ;;
esac

exit 0

# End network
network-interface sysvinit
  • 1 个回答
  • 75 Views
Martin Hope
Muhammad Ikhwan Perwira
Asked: 2022-05-21 06:22:06 +0800 CST

为什么 dmesg 输出显示在 /dev/tty1 上

  • 0

这仍然与我昨天的线程有关,所以有警报或者它只是显示在我的终端上的日志/dev/tty1。当然这很烦人,因为它显示在我的 bash 提示符中,所以每当我想输入一些东西时,我的输入都会被该输出覆盖。它可能会定期打印出来3 second。所以你可以看到它有多烦人

我的终端看起来像这样:

root@LFS:# echo "Hey get out of there"clocksource: timekeeping watchdog on CPU0: acpi_pm wd-wd readback delay of 643744ns
clocksource: wd-tsc-wd read-back delay of 182144ns, clock-skew test skipped!
clocksource: timekeeping watchdog on CPU0: acpi_pm wd-wd readback delay of 643744ns
clocksource: wd-tsc-wd read-back delay of 182144ns, clock-skew test skipped!
clocksource: timekeeping watchdog on CPU0: acpi_pm wd-wd readback delay of 643744ns
clocksource: wd-tsc-wd read-back delay of 182144ns, clock-skew test skipped!
...

我怀疑这不是因为clocksource,而是因为dmesg输出。因为当我指挥dmesg. 它显示相同。但是,每当我使用/dev/pts没有烦人的输出或定期发出警报时,我都会在我的 LFS 系统中登录 SSH 时对其进行测试。

那么如何防止dmesg日志显示到/dev/tty1

更新:内部/proc/cmdline

root@LFS:~# cat /proc/cmdline
BOOT_IMAGE=/boot/vmlinuz-5.16.9-lfs-11.1 root=/dev/sda1 ro
tty stdout
  • 1 个回答
  • 235 Views
Martin Hope
Muhammad Ikhwan Perwira
Asked: 2022-05-20 17:16:40 +0800 CST

定期在 bash 提示符中出现烦人的时钟源警报/警告

  • 0

所以我只是建立了 LFS 系统,它工作正常,但令人讨厌的部分是每个周期显示像每 3 秒一样的警报。它说

root@LFS:# clocksource: timekeeping watchdog on CPU0: acpi_pm wd-wd readback delay of 643744ns
clocksource: wd-tsc-wd read-back delay of 182144ns, clock-skew test skipped!
clocksource: timekeeping watchdog on CPU0: acpi_pm wd-wd readback delay of 643744ns
clocksource: wd-tsc-wd read-back delay of 182144ns, clock-skew test skipped!
clocksource: timekeeping watchdog on CPU0: acpi_pm wd-wd readback delay of 643744ns
clocksource: wd-tsc-wd read-back delay of 182144ns, clock-skew test skipped!
...

这让我的 bash 提示很烦,我什至看不到 bash 提示的输出

kernel lfs
  • 1 个回答
  • 330 Views
Martin Hope
Muhammad Ikhwan Perwira
Asked: 2022-05-15 03:53:39 +0800 CST

提取 tar 然后进入其目录(shell 脚本)

  • 0

如何使脚本提取 tar 然后进入提取的 tar 目录?

我在尝试

提取物

#!/bin/bash
tar -xvf $1 && cd $1

使用示例

假设有foo.tar.xz 那么如何使用它:./xtract foo.tar.xz

所以你可以看到我的问题在于参数,我成功提取它但由于文件扩展名而无法输入它。那么如何在参数中删除它以便我可以进入foo目录

shell-script scripting
  • 1 个回答
  • 158 Views
Martin Hope
Muhammad Ikhwan Perwira
Asked: 2022-02-11 11:39:37 +0800 CST

日志文件中的日期字符串列到时间戳列

  • 0

我有这样log.txt的示例输出:

...
10-Feb-2022 15:15:14.099 lorem
10-Feb-2022 15:15:15.133 ipsum
10-Feb-2022 15:15:16.233 dolor
...

我希望过滤后的 log.txt 的输出是

...
1644480914 lorem
1644480915 ipsum
1644480916 dolor
...

我已经想出了如何将日期字符串转换为时间戳

date --date='10-Feb-2022 15:15:14.099' +"%s"
Output:
1644480914

我的大脑仍然不明白如何将该日期命令应用于 log.txt。

另外我如何管道该日期命令?

printf "10-Feb-2022 15.15.17.012 water" | date --date=<what must i put here?> + "%s"

我希望流水线命令的输出是相同的,不同的是不干扰其他列,即1644480917 water

logs date
  • 1 个回答
  • 173 Views
Martin Hope
Muhammad Ikhwan Perwira
Asked: 2022-02-11 05:14:47 +0800 CST

定期过滤更改文件并将过滤后的输出重定向到新文件

  • 0

假设我有log.txt

log.txt 的样本格式如下: Code Data Timestamp

...
C:57 hello 1644498429
C:56 world 1644498430
C:57 water 1644498433
...

如果我想要包含的过滤字符串行,C:57我可以用

cat log.txt | grep C:57

然后我将输出重定向到新文件,因此

cat log.txt | grep C:57 > filtered_log.txt

但是,当 log.txt 有新变化时,我应该再次重复执行该命令。我希望它定期执行或针对文件中的每个新更改执行,或者仅在有包含 string 的新行时执行C:57。

grep read
  • 1 个回答
  • 97 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve